SakuraSnow's blog SakuraSnow's blog
首页
  • JavaScript
  • TypeScript
  • Vue
  • React
  • Git
  • Node
  • Linux
  • 技术文档
  • 博客搭建
  • 数据结构
  • leetcode
  • 关于
  • 友链
  • 收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

SakuraSnow

一只前端咸鱼
首页
  • JavaScript
  • TypeScript
  • Vue
  • React
  • Git
  • Node
  • Linux
  • 技术文档
  • 博客搭建
  • 数据结构
  • leetcode
  • 关于
  • 友链
  • 收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 数据结构

  • Leetcode

    • 001-Two Sum[两数之和]
    • 002-Add Two Numbers[两数相加].md
      • 003-Longest Substring Without Repeating Characters[无重复字符的最长子串]
      • 004-Reverse-integer[整数反转]
      • 008-String to Integer (atoi)[字符串转整数]
      • 009-Palindrome Number[回文数]
    • 算法
    • Leetcode
    SakuraSnow
    2022-03-05

    002-Add Two Numbers[两数相加].md

    # 题目

    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 contains a single digit. Add the two numbers and return the sum as a linked list.

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

    # example

    Input: l1 = [2,4,3], l2 = [5,6,4]
    Output: [7,0,8]
    Explanation: 342 + 465 = 807.
    
    1
    2
    3
    Input: l1 = [0], l2 = [0]
    Output: [0]
    
    1
    2
    Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
    Output: [8,9,9,9,0,0,0,1]
    
    1
    2

    # 我的思路

    
    
    //leetcode submit region begin(Prohibit modification and deletion)
    
    // class ListNode {
    //     val: number
    //     next: ListNode | null
    //     constructor(val?: number, next?: ListNode | null) {
    //         this.val = (val === undefined ? 0 : val)
    //         this.next = (next === undefined ? null : next)
    //     }
    // }
    
    // function buildBinaryTree(arr: Array<number>): ListNode|null {
    //     let head = new ListNode(arr[0]);
    //     let cur = head;
    //     arr.slice(1).forEach(element => {
    //         cur.next = new ListNode(element);
    //         cur = cur.next;
    //     });
    //     return head;
    // }
    
    
    function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
        // 扔两个指针
        let ptr1: ListNode | null = l1,
            ptr2: ListNode | null = l2;
        // head ptr
        // 弄个head出来 方便返回 而且循环里可以少写几行代码(不用判空)
        let head = new ListNode(), ptr = head;
        // 进位
        let t = 0;
        // 全部为null时stop
        while (ptr1 || ptr2) {
            ptr.next = new ListNode();
            ptr = ptr.next;
            // 当前位总和
            let sum = ((ptr1 ? ptr1.val : 0) + (ptr2 ? ptr2.val : 0)) + t;
            // 当前位
            let cur = sum % 10;
            // 存储进位
            t = Number.parseInt(String(sum / 10));
            // 存储当前位
            ptr.val = cur;
            ptr1 = ptr1 && ptr1.next;
            ptr2 = ptr2 && ptr2.next;
        }
        // 如果t最后是1 需要再创建一个节点加上去
        if (t) {
            ptr.next = new ListNode(t);
        }
        return head.next;
    };
    // 测试代码
    // console.log(addTwoNumbers(buildBinaryTree([2,4,3]), buildBinaryTree([5,6,4])));
    // console.log(addTwoNumbers(buildBinaryTree([9,9,9,9,9,9,9]), buildBinaryTree([9,9,9,9])))
    // console.log(addTwoNumbers(buildBinaryTree([0]), buildBinaryTree([0])))
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59

    # 最优解

    最优解应该是我这个了,但是我平时绝对不会写这种可读性这么垃圾的代码

    肯定是new一个数组,在数组里加完再转成链表

    另外这个思路也不错,起码短

    /**
     * @param {ListNode} l1
     * @param {ListNode} l2
     * @return {ListNode}
     */
    var addTwoNumbers = function(l1, l2) {
        let s1 = JSON.stringify(l1).match(/\d/g).reverse().join(''),
            s2 = JSON.stringify(l2).match(/\d/g).reverse().join('')
    
        sum = BigInt(s1)+BigInt(s2)
    
        return [...sum.toString()].reduce((acc,v)=>({val: v, next: acc}), null)
    };
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #算法#Leetcode#链表
    上次更新: 2022/03/09, 15:16:52
    001-Two Sum[两数之和]
    003-Longest Substring Without Repeating Characters[无重复字符的最长子串]

    ← 001-Two Sum[两数之和] 003-Longest Substring Without Repeating Characters[无重复字符的最长子串]→

    最近更新
    01
    009-Palindrome Number[回文数]
    03-10
    02
    008-String to Integer (atoi)[字符串转整数]
    03-10
    03
    004-Reverse-integer[整数反转]
    03-09
    更多文章>
    Theme by Vdoing | Copyright © 2019-2022 Evan Xu | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式
    ×