LeetCode #002# Add Two Numbers(js描述)

索引

    1. 思路1:基本加法规则
    2. 思路2:移花接木法。。。

问题描述:https://leetcode.com/problems/add-two-numbers/

思路1:基本加法规则

根据小学学的基本加法规则。。。。。我们需要将两个数以最低位为基准对齐,然后逐个加,需要进位的给进位就行了。。。。恰好这个链表是逆序的!!!已经为我们对齐了。用两个指针分别指向两个链表头,开始同步往后移动,边移动边计算结果,直到两个指针都到了尽头/没有进位。时间复杂度是O(max(m, n)),空间复杂度和时间复杂度一样。js代码如下:

// Runtime: 116 ms
var addTwoNumbers = function(l1, l2) {
    let dummy = { next: null }, // 结果链表的head指针
        tail = dummy, // tail总是指向dummy的末尾元素,以便在链表尾插入新元素
        flag = false, // flag标示是否需要进位
        sum;

    let init = () => { // 初始化,因为保证非空,所以首位总是存在的(可能为0)
        sum = l1.val + l2.val;
        tail.next = new ListNode(sum % 10);
        // 为下一位的计算做准备
        flag = sum >= 10;
        l1 = l1.next;
        l2 = l2.next;
        // 保持tail性质不变
        tail = tail.next;
    };

    let work = () => {
        let v1, v2;
        while (l1 || l2 || flag) {
            // 统一化处理,不用判断谁空谁不空
            v1 = l1 ? l1.val : 0;
            v2 = l2 ? l2.val : 0;
            sum = v1 + v2 + (flag ? 1 : 0);
            tail.next = new ListNode(sum % 10);
            // 为下一位的计算做准备
            flag = sum >= 10;
            l1 = l1 ? l1.next : null;
            l2 = l2 ? l2.next : null;
            // 保持tail性质不变
            tail = tail.next;
        }
    };

    init();
    work();

    return dummy.next;
};

思路2:移花接木法。。。

基本上和思路1没区别,并不能改善时间复杂度的渐进性,只是省了点空间。js代码:

// 不创建新链表,直接把结果存到l1上,并对多出来的部分做"嫁接"处理
// Runtime: 112 ms, faster than 99.52% of JavaScript online submissions for Add Two Numbers.
var addTwoNumbers2 = function(l1, l2) {
    let dummy = { next: l1 }, // 结果链表的head指针
        tail = dummy, // tail总是指向l1的前继元素(也就是dummy的尾元素),以便在链尾插入链表/新元素
        flag = false, // flag标示下一位的计算是否需要进位
        sum;

    let init = () => { // 初始化,因为保证非空,所以首位总是存在的
        sum = l1.val + l2.val;
        l1.val = sum % 10;
        // 为下一位的计算做准备
        flag = sum >= 10;
        l1 = l1.next;
        l2 = l2.next;
        // 保持tail性质不变
        tail = tail.next;
    };

    let status1 = () => { // 处理等长的那部分
        while (l1 && l2) {
            sum = l1.val + l2.val + (flag ? 1 : 0);
            l1.val = sum % 10;
            // 为下一位的计算做准备
            flag = sum >= 10;
            l1 = l1.next;
            l2 = l2.next;
            // 保持tail性质不变
            tail = tail.next;
        }
    };

    let status2 = () => { // 处理多出来的部分
        // 如果l2更长,直接移植到l1
        if (l2) {
            l1 = tail.next = l2;
        }
        // 处理进位
        while (l1 && flag) {
            sum = l1.val + 1;
            l1.val = sum % 10;
            // 为下一位的计算做准备
            flag = sum >= 10;
            l1 = l1.next;
            // 保持tail性质不变
            tail = tail.next;
        }
        if (flag) {
            tail.next = new ListNode(1);
        }
    };

    init();
    status1();
    status2();

    return dummy.next;
};

原文地址:https://www.cnblogs.com/xkxf/p/10226056.html

时间: 2024-09-29 07:57:12

LeetCode #002# Add Two Numbers(js描述)的相关文章

leetcode 002 Add Two Numbers

Add Two Numbers My Submissions Question Total Accepted: 105768 Total Submissions: 494750 Difficulty: Medium You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a

[LeetCode] 002. Add Two Numbers (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 002.Add_Two_Numbers (Medium) 链接: 题目:https://oj.leetcode.com/problems/add-two-numbers/ 代码(github):https://github.com/illuz/leetcode 题意: 求两个 List 相加产生的新的一个 List

LeetCode 002 Add Two Numbers - Java

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

Java for LeetCode 002 Add Two Numbers

You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】002 Add Two Numbers

题目:LeetCode 002 Add Two Numbers 题意:给定表达非负数的两个链表,这些数字按照反向顺序存储,每个节点包含一个单独的数字,将这两个数相加,返回一个新链表. 样例: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 链表每个节点的结构: 1 struct ListNode { 2 int val; 3 ListNode *next; 4 ListNode(int x) : val(

LeetCode --- 2. Add Two Numbers

题目链接:Add Two Numbers You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3) + (5 -> 6 ->

LeetCode:Add Two Numbers - 两个链表逐项做带进位的加法生成新链表

1.题目名称 Add Two Numbers (两个链表逐项做带进位的加法生成新链表) 2.题目地址 https://leetcode.com/problems/add-two-numbers/ 3.题目内容 英文:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a

【leetcode】Add Two Numbers 解析以及拓展

题目: You are given two linked lists representing two non-negative numbers. 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. Input: (2 -> 4 -> 3) + (5 -> 6 -&