LeetCode-2AddTwoNumbers(C#)

# 题目

2. 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 -> 4)
Output: 7 -> 0 -> 8

# 思路

不知道各位能不能看懂题目,简单解释一下,就是把整数每一位颠倒进行加法。题目中给出的例子,最初对应342 + 465 = 807,然后颠倒变成243 + 564 = 708,在转换为链表。

这下题目给出链表的定义,我们需要对这种类型的链表进行操作。

        // Definition for singly-linked list.
        public class ListNode
        {
            public int val;
            public ListNode next;
            public ListNode(int x) { val = x; }
        }

方法一:普通遍历,链表l1和l2相应位置相加,再加进位,存入链表result中。

注意点:

  1. 对于一段长于另外一段的链表部分,单独处理。
  2. 进位。
  3. 结果之和长于两个链表的情况,如1 + 999 = 1000。

普通遍历时间复杂度O(n)空间复杂度O(n)时间204ms

        // normal traversal: time O(n) space O(n) result: 204ms
        public void calculateSum(ListNode tresult, ref int carry, int sum)
        {
            if (sum >= 10)
            {
                carry = 1;
                tresult.next = new ListNode(sum - 10);
            }
            else
            {
                carry = 0;
                tresult.next = new ListNode(sum);
            }
        }

        public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
        {
            ListNode tl1 = l1, tl2 = l2;
            ListNode result = new ListNode(0);
            ListNode tresult = result;
            int carry = 0;

            // both ListNode 1 and ListNode 2 have values
            while (tl1 != null && tl2 != null)
            {
                calculateSum(tresult, ref carry, tl1.val + tl2.val + carry);
                tl1 = tl1.next;
                tl2 = tl2.next;
                tresult = tresult.next;
            }

            // Debug.Assert(!(tl1 != null && tl2 != null), "tl1 and tl2 aren‘t null");
            // either ListNode 1 or ListNode 2 has values (maybe) and don‘t forget carry.
            while (tl1 != null)
            {
                calculateSum(tresult, ref carry, tl1.val + carry);
                tl1 = tl1.next;
                tresult = tresult.next;
            }
            while (tl2 != null)
            {
                calculateSum(tresult,ref carry, tl2.val + carry);
                tl2 = tl2.next;
                tresult = tresult.next;
            }

            // at this time, ListNode 1 and ListNode 2 should be null, however, carry could be null or not
            // Debug.Assert(tl1 == null && tl2 == null, "calculation doesn‘t finish");
            if (carry == 1) tresult.next = new ListNode(1);

            // neither ListNode 1 nor ListNode 2 have values
            return result.next;
        }
        */

方法二:空间优化遍历,链表l1和l2相应位置相加,再加进位,存入链表l1中。方法二的代码没有方法一的代码清晰。

空间优化遍历时间复杂度O(n)空间复杂度O(1)时间208ms

        // use ListNode 1 to restore result
        // space (and time, I think, but result doesn‘t prove) optimized traversal: time O(n) space O(1) result: 208ms
        public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
        {
            if (l1 == null) return l2;
            if (l2 == null) return l1;

            int carry = 0, sum = 0;
            ListNode pre = null, result = l1;
            while (l1 != null || l2 != null || carry != 0)
            {
                // calculate sum and carry
                sum = 0;
                if (l1 != null) sum += l1.val;
                if (l2 != null)
                {
                    sum += l2.val;
                    l2 = l2.next; // ListNode1 will be used below, ListNode2 not, so if ListNode 2 next exists, ListNode 2 move to next
                }
                sum += carry;
                if (sum >= 10)
                {
                    carry = 1;
                    sum -= 10;
                }
                else
                {
                    carry = 0;
                }

                // find a place for sum in ListNode 1, l1 is in use
                if (l1 != null)
                {
                    pre = l1;
                    if (sum >= 10) sum -= 10;
                    l1.val = sum;
                    l1 = l1.next;
                }
                else
                {
                    if (sum >= 10) sum -= 10;
                    pre.next = new ListNode(sum);
                    pre = pre.next;
                }

            }
            return result;
        }
        */

方法三:递归,链表l1和l2相应位置相加,再加进位,存入链表node中。速度最快,是比较好的解决方案。

# 解决(递归)

递归时间复杂度O(n)空间复杂度O(n)时间196ms

        // recursive tranversal: time O(n) space:O(n) time: 196ms (why it is faster than normal loop)
        public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
        {
            return AddTwoNumbers(l1, l2, 0);
        }

        public ListNode AddTwoNumbers(ListNode l1, ListNode l2, int carry)
        {
            if (l1 == null && l2 == null && carry == 0) return null;

            // calculate sum
            int sum = 0;
            if (l1 != null) sum += l1.val;
            if (l2 != null) sum += l2.val;
            sum += carry;
            if (sum >= 10)
            {
                carry = 1;
                sum -= 10;
            }
            else
            {
                carry = 0;
            }

            // set node‘s next and val and return
            ListNode node = new ListNode(sum);
            node.next = AddTwoNumbers(l1 != null ? l1.next : null, l2 != null ? l2.next : null, carry);
            return node;
        }

# 题外话

为何递归会比循环快呢?百思不得其解,若有高人知道,请指教。

# 测试用例

        static void Main(string[] args)
        {
            _2AddTwoNumbers solution = new _2AddTwoNumbers();
            ListNode l1 = new ListNode(1);
            ListNode l2 = new ListNode(2);
            ListNode result = new ListNode(3);

            // ListNode doesn‘t have a null constructor, so we can igonore this case
            Debug.Assert(Test.match(solution.AddTwoNumbers(l1, l2), result), "wrong 1");

            // ListNode 1 length is larger than ListNode 2 length
            l1 = new ListNode(1);
            l1.next = new ListNode(4);
            l1.next.next = new ListNode(5);
            l2 = new ListNode(2);
            result.next = new ListNode(4);
            result.next.next = new ListNode(5);
            Debug.Assert(Test.match(solution.AddTwoNumbers(l1, l2), result), "wrong 2");

            // ListNode 2 length is larger than ListNode 1 length and has carries
            l1 = new ListNode(1);
            l1.next = new ListNode(1);
            l2 = new ListNode(9);
            l2.next = new ListNode(8);
            l2.next.next = new ListNode(9);
            result = new ListNode(0);
            result.next = new ListNode(0);
            result.next.next = new ListNode(0);
            result.next.next.next = new ListNode(1);
            Debug.Assert(Test.match(solution.AddTwoNumbers(l1, l2), result), "wrong 3");
        }
  class Test
  {

        public static bool match(_2AddTwoNumbers.ListNode l1, _2AddTwoNumbers.ListNode l2)
        {
            _2AddTwoNumbers.ListNode tl1 = l1, tl2 = l2;
            while(tl1 != null && tl2 != null)
            {
                if (tl1.val != tl2.val) return false;
                tl1 = tl1.next;
                tl2 = tl2.next;
            }
            if (tl1 == null && tl2 == null) return true;
            else return false;
        }

  }

# 地址

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

A: https://github.com/mofadeyunduo/LeetCode/tree/master/2AddTwoNumbers

(希望各位多多支持本人刚刚建立的GitHub和博客,谢谢,有问题可以邮件[email protected]或者留言,我尽快回复)

时间: 2024-10-01 07:02:49

LeetCode-2AddTwoNumbers(C#)的相关文章

[LeetCode] 349 Intersection of Two Arrays & 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

[LeetCode]Count Primes

题目:Count Primes 统计1-n的素数的个数. 思路1: 通常的思想就是遍历(0,n)范围内的所有数,对每个数i再遍历(0,sqrt(i)),每个除一遍来判断是否为素数,这样时间复杂度为O(n*sqrt(n)). 具体实现不在贴代码,过程很简单,两重循环就可以解决.但是效率很差,n较大时甚至会花几分钟才能跑完. 思路2: 用埃拉特斯特尼筛法的方法来求素数,时间复杂度可以达到O(nloglogn). 首先开一个大小为n的数组prime[],从2开始循环,找到一个质数后开始筛选出所有非素数

[LeetCode]Repeated DNA Sequences

题目:Repeated DNA Sequences 给定包含A.C.G.T四个字符的字符串找出其中十个字符的重复子串. 思路: 首先,string中只有ACGT四个字符,因此可以将string看成是1,3,7,20这三个数字的组合串: 并且可以发现{ACGT}%5={1,3,2,0};于是可以用两个位就能表示上面的四个字符: 同时,一个子序列有10个字符,一共需要20bit,即int型数据类型就能表示一个子序列: 这样可以使用计数排序的思想来统计重复子序列: 这个思路时间复杂度只有O(n),但是

Leetcode 给一个数a和一个向量b,找出该向量b中的2个数相加等于a,并输出这两个数在向量中的位置

看C++primer Plus看的无聊,第一次做Leetcode的练习,本来想做二维向量的,结果始终通不过,查了原因,必须用一维的... 一维的答案: class Solution {  public:   vector<int> twoSum(vector<int>& nums, int target)   {    int cout = nums.size();  //得到向量的大小    vector<int>ret;  //定义向量      for (