xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js & array to string All In One

js & array to string All In One

https://stackoverflow.com/questions/13272406/convert-string-with-commas-to-array

js string to array

`807`.split('').map(Number).reverse();
// [7, 0, 8]

https://leetcode.com/problems/add-two-numbers/

// TODO...
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function(l1, l2) {
    let v1 = l1.val;
    let node1 = l1;
    while(node1.next) {
      node1 = node1.next;
      v1 += node1.val;
    }
    let v2 = l2.val;
    let node2 = l1;
    while(node2.next) {
      node2 = node2.next;
      v2 += node2.val;
    }
    // 反转
    // let arr = `${v1 + v2}`.split('').map(Number).reverse();
    // `807`.split('').map(Number);
    // const newList = new ListNode(v1 + v2, null);
    // let node = newList;
    // while(arr.length) {
    //    arr.pop();
    //    newList.next = new ListNode(v1 + v2, null);
    // }
    // return newList;
    let arr = `${v1 + v2}`.split('').map(Number);
    console.log('arr ', arr);
    let sum = v1 + v2;
    if(sum === 0) {
       return new ListNode(0);
    }
    let newList = new ListNode(arr[0]);
    arr.shfit();
    while(arr.length) {
       const value = arr.shfit();
       newList.next = new ListNode(value);
    }
    return newList;
};


refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2019-01-10 22:44  xgqfrms  阅读(743)  评论(2编辑  收藏  举报