美文网首页
Add Two Numbers (LeetCode)

Add Two Numbers (LeetCode)

作者: 静候那一米阳光 | 来源:发表于2017-05-17 19:07 被阅读0次

    Add Two Numbers (LeetCode)

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

    题目

    You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

    You may assume the two numbers do not contain any leading zero, except the number 0 itself.

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8

    答案

    思路

    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    function ListNode(val) {
        this.val = val;
        this.next = null;
    }
    /**
     * @param {ListNode} l1
     * @param {ListNode} l2
     * @return {ListNode}
     */
    var addTwoNumbers = function(l1, l2) {
        var n1, n2, d = 0,
            v1, v2, o; //d进位,o当前位,n1 n1当前节点,v1 v2当前节点的值
        n1 = l1;
        n2 = l2;
        v1 = n1 ? n1.val : 0;
        v2 = n2 ? n2.val : 0;
        o = (v1 + v2 + d) % 10; //当前位
        d = Math.floor((v1 + v2 + d) / 10);
        var res = new ListNode(o);
        var node = res;
        while (n1 || n2 || d) {
            n1 = n1 ? n1.next : null;
            n2 = n2 ? n2.next : null;
            if (!n1 && !n2 && !d) break;
            v1 = n1 ? n1.val : 0;
            v2 = n2 ? n2.val : 0;
            o = (v1 + v2 + d) % 10; //当前位
            d = Math.floor((v1 + v2 + d) / 10);
            node.next = new ListNode(o);
            node = node.next;
        }
        return res;
    };
    
    var l1 = new ListNode(2);
    l1.next = new ListNode(4);
    l1.next.next = new ListNode(3);
    // l1.next.next.next = new ListNode(5);
    var l2 = new ListNode(5);
    l2.next = new ListNode(6);
    l2.next.next = new ListNode(4);
    console.log(addTwoNumbers(l1, l2))
    

    核心方法进阶

    var addTwoNumbers = function(l1, l2) {
        var n1, n2, d = 0,
            v1, v2, o; //d进位,o当前位,n1 n1当前节点,v1 v2当前节点的值
        n1 = l1;
        n2 = l2;
        var res = new ListNode(0);
        var node=res;
        while (n1 || n2 || d) {
            v1 = n1 ? n1.val : 0;
            v2 = n2 ? n2.val : 0;
            o = (v1 + v2 + d) % 10; //当前位
            d = Math.floor((v1 + v2 + d) / 10);
            node.next=new ListNode(o);
            node=node.next;
            n1 = n1?n1.next:null;
            n2 = n2?n2.next:null;
        }
        return res.next;
    };
    

    核心方法优化

    var addTwoNumbers = function(l1, l2) {
        //d进位,o当前位,l1 l1当前节点,v1 v2当前节点的值
        var d = 0,
            v1, v2, o,
            res = new ListNode(0),
            node = res;
        while (l1 || l2 || d) {
            v1 = l1 ? l1.val : 0;
            v2 = l2 ? l2.val : 0;
            o = (v1 + v2 + d) % 10; //当前位
            d = Math.floor((v1 + v2 + d) / 10);
            node.next = new ListNode(o);
            node = node.next;
            l1 = l1 ? l1.next : null;
            l2 = l2 ? l2.next : null;
        }
        return res.next;
    };
    

    继续优化

    var addTwoNumbers = function(l1, l2) {
        var d = 0,//进位
            res = {},
            node = res;
        while (l1 || l2 || d) {
            var v = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + d;//当前值+进位
            d = Math.floor(v / 10);
            node = node.next = new ListNode(v % 10);
            l1 = l1 ? l1.next : null;
            l2 = l2 ? l2.next : null;
        }
        return res.next;
    };
    

    相关文章

      网友评论

          本文标题:Add Two Numbers (LeetCode)

          本文链接:https://www.haomeiwen.com/subject/nntjxxtx.html