445. 两数相加 II
Posted zouma
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了445. 两数相加 II相关的知识,希望对你有一定的参考价值。
非进阶版本的三次翻转链表,这道题成功让我明白了我的脑子是有问题的,是没有逻辑可言的,做算法题是面向错误答案编程的。下次先要写伪代码才合理一点。
1 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 2 if (l1 == nullptr) 3 return l2; 4 else if (l2 == nullptr) 5 return l1; 6 ListNode* dummy1 = nullptr; 7 ListNode* dummy2 = nullptr; 8 ListNode* temp1 = l1; 9 ListNode* temp2 = l2; 10 ListNode* backup1 = new ListNode(0); 11 while (temp1 != nullptr) 12 { 13 temp1 = temp1->next; 14 l1->next = dummy1; 15 dummy1 = l1; 16 l1 = temp1; 17 } 18 l1 = dummy1; 19 while (temp2 != nullptr) 20 { 21 temp2 = temp2->next; 22 l2->next = dummy2; 23 dummy2 = l2; 24 l2 = temp2; 25 } 26 l2 = dummy2; 27 28 bool carry = false; 29 temp1 = l1; 30 temp2 = l2; 31 ListNode* pre_l1 = nullptr; 32 ListNode* pre_l2 = nullptr; 33 while (temp1!=nullptr && temp2!=nullptr) //把新结果放在 l1中 34 { 35 if (carry == false && temp1->val + temp2->val < 10) //之前无进位,相加小于10 36 { 37 temp1->val = temp1->val + temp2->val; 38 } 39 else if (carry == false && temp1->val + temp2->val >= 10) //之前无进位,相加大于等于10 40 { 41 carry = true; 42 temp1->val = temp1->val + temp2->val - 10; 43 } 44 else if(carry == true && temp1->val + temp2->val >= 9 ) //之前有进位,相加大于等于9 45 { 46 carry = true; 47 temp1->val = temp1->val + temp2->val +1 - 10; 48 } 49 else //之前有进位,相加小于9 50 { 51 carry = false; 52 temp1->val = temp1->val + temp2->val+1; 53 } 54 pre_l1 = temp1; 55 temp1 = temp1->next; 56 pre_l2 = temp2; 57 temp2 = temp2->next; 58 } 59 60 if (temp1 == nullptr&&temp2 == nullptr&&carry==true) 61 { 62 carry = false; 63 backup1->val = 1; 64 backup1->next = nullptr; 65 pre_l1->next = backup1; 66 } 67 else if (temp1 == nullptr&&carry == true) 68 { 69 pre_l1->next = temp2; 70 while (temp2) 71 { 72 if (carry == true) 73 { 74 temp2->val += 1; 75 carry = false; 76 } 77 if (temp2->val >= 10) 78 { 79 carry = true; 80 temp2->val -= 10; 81 } 82 pre_l2 = temp2; 83 temp2 = temp2->next; 84 } 85 if (carry == true) 86 { 87 backup1->val = 1; 88 backup1->next = nullptr; 89 pre_l2->next = backup1; 90 } 91 } 92 else if (temp2==nullptr&&carry==true) 93 { 94 while (temp1) 95 { 96 if (carry == true) 97 { 98 temp1->val += 1; 99 carry = false; 100 } 101 102 if (temp1->val >= 10) 103 { 104 carry = true; 105 temp1->val -= 10; 106 } 107 pre_l1 = temp1; 108 temp1 = temp1->next; 109 } 110 if (carry == true) 111 { 112 backup1->val = 1; 113 backup1->next = nullptr; 114 pre_l1->next = backup1; 115 } 116 } 117 else if (temp2 != nullptr&&carry==false) 118 { 119 pre_l1->next = temp2; 120 } 121 ListNode* dummy3 = nullptr; 122 ListNode* temp3 = l1; 123 while (temp3 != nullptr) 124 { 125 temp3 = temp3->next; 126 l1->next = dummy3; 127 dummy3 = l1; 128 l1 = temp3; 129 } 130 l1 = dummy3; 131 132 return l1; 133 }
执行用时 :32 ms, 在所有 C++ 提交中击败了90.63%的用户
内存消耗 :9.8 MB, 在所有 C++ 提交中击败了99.54%的用户
祭奠我逝去的两个小时。。。
至于题解中看到的用栈,感觉学不到很多,不如用递归,虽然好像没什么区别,但用栈有点无脑。。虽然递归也是用到的栈
递归解法
1 int length(ListNode* l1) 2 { 3 if (l1 == nullptr) 4 return 0; 5 else 6 return 1 + length(l1->next); 7 } 8 int addTwoNumbersHelper(ListNode* l1, ListNode* l2, ListNode* &res) 9 { 10 if (l1 == nullptr || l2 == nullptr) 11 { 12 return 0; 13 } 14 int sum = l1->val + l2->val + addTwoNumbersHelper(l1->next, l2->next,res); 15 ListNode* newhead = new ListNode(sum % 10); 16 newhead->next = res; 17 res = newhead; 18 return sum / 10; 19 } 20 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 21 if (l1 == nullptr) 22 return l2; 23 else if (l2 == nullptr) 24 return l1; 25 int m = length(l1); 26 int n = length(l2); 27 28 ListNode* res = nullptr; 29 ListNode* dummy = new ListNode(0); 30 int carry = 0; 31 if (m > n) 32 { 33 int k = m - n; 34 dummy->next = l2; 35 while (k--) 36 { 37 ListNode* makeup = new ListNode(0); 38 makeup->next = dummy->next; 39 dummy->next = makeup; 40 } 41 carry = addTwoNumbersHelper(l1, dummy->next, res); 42 } 43 else if (m < n) 44 { 45 int k = n - m; 46 dummy->next = l1; 47 while (k--) 48 { 49 ListNode* makeup = new ListNode(0); 50 makeup->next = dummy->next; 51 dummy->next = makeup; 52 } 53 carry = addTwoNumbersHelper(dummy->next, l2, res); 54 } 55 else 56 { 57 carry = addTwoNumbersHelper(l1, l2, res); 58 } 59 if (carry > 0) 60 { 61 ListNode* makeup = new ListNode(carry); 62 makeup->next = res; 63 res = makeup; 64 } 65 return res; 66 }
以上是关于445. 两数相加 II的主要内容,如果未能解决你的问题,请参考以下文章