Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit edc2ea8

Browse files
feat: update solutions to lcci problem: No.02.05 (doocs#2322)
No.02.05.Sum Lists
1 parent 1ab2686 commit edc2ea8

File tree

6 files changed

+65
-49
lines changed

6 files changed

+65
-49
lines changed

‎lcci/02.05.Sum Lists/README.md‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@
2929

3030
## 解法
3131

32-
### 方法一
32+
### 方法一:模拟
33+
34+
我们同时遍历两个链表 $l_1$ 和 $l_2,ドル并使用变量 $carry$ 表示当前是否有进位。
35+
36+
每次遍历时,我们取出对应链表的当前位,计算它们与进位 $carry$ 的和,然后更新进位的值,最后将当前位的值加入答案链表。如果两个链表都遍历完了,并且进位为 0ドル$ 时,遍历结束。
37+
38+
最后我们返回答案链表的头节点即可。
39+
40+
时间复杂度 $O(\max(m, n)),ドル其中 $m$ 和 $n$ 分别为两个链表的长度。我们需要遍历两个链表的全部位置,而处理每个位置只需要 $O(1)$ 的时间。忽略答案的空间消耗,空间复杂度 $O(1)$。
3341

3442
<!-- tabs:start -->
3543

@@ -43,15 +51,15 @@
4351

4452
class Solution:
4553
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
46-
dummy = cur =ListNode(0)
47-
carry= 0
54+
dummy = ListNode()
55+
carry, curr = 0, dummy
4856
while l1 or l2 or carry:
49-
carry += (0 if notl1 else l1.val) + (0 if notl2 else l2.val)
50-
cur.next = ListNode(carry % 10)
51-
cur = cur.next
52-
carry //=10
53-
l1 = None if notl1 else l1.next
54-
l2 = None if notl2 else l2.next
57+
s = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry
58+
carry, val =divmod(s, 10)
59+
curr.next = ListNode(val)
60+
curr = curr.next
61+
l1 = l1.next if l1 else None
62+
l2 = l2.next if l2 else None
5563
return dummy.next
5664
```
5765

@@ -66,8 +74,8 @@ class Solution:
6674
*/
6775
class Solution {
6876
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
77+
ListNode dummy = new ListNode(0);
6978
int carry = 0;
70-
ListNode dummy = new ListNode(-1);
7179
ListNode cur = dummy;
7280
while (l1 != null || l2 != null || carry != 0) {
7381
int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;

‎lcci/02.05.Sum Lists/README_EN.md‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@
3232

3333
## Solutions
3434

35-
### Solution 1
35+
### Solution 1: Simulation
36+
37+
We traverse two linked lists $l_1$ and $l_2$ simultaneously, and use a variable $carry$ to indicate whether there is a carry-over currently.
38+
39+
During each traversal, we take out the current digit of the corresponding linked list, calculate the sum of them and the carry-over $carry,ドル then update the value of the carry-over, and finally add the value of the current digit to the answer linked list. The traversal ends when both linked lists have been traversed and the carry-over is 0ドル$.
40+
41+
Finally, we return the head node of the answer linked list.
42+
43+
The time complexity is $O(\max(m, n)),ドル where $m$ and $n$ are the lengths of the two linked lists respectively. We need to traverse all positions of the two linked lists, and it only takes $O(1)$ time to process each position. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
3644

3745
<!-- tabs:start -->
3846

@@ -46,15 +54,15 @@
4654

4755
class Solution:
4856
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
49-
dummy = cur =ListNode(0)
50-
carry= 0
57+
dummy = ListNode()
58+
carry, curr = 0, dummy
5159
while l1 or l2 or carry:
52-
carry += (0 if notl1 else l1.val) + (0 if notl2 else l2.val)
53-
cur.next = ListNode(carry % 10)
54-
cur = cur.next
55-
carry //=10
56-
l1 = None if notl1 else l1.next
57-
l2 = None if notl2 else l2.next
60+
s = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry
61+
carry, val =divmod(s, 10)
62+
curr.next = ListNode(val)
63+
curr = curr.next
64+
l1 = l1.next if l1 else None
65+
l2 = l2.next if l2 else None
5866
return dummy.next
5967
```
6068

@@ -69,8 +77,8 @@ class Solution:
6977
*/
7078
class Solution {
7179
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
80+
ListNode dummy = new ListNode(0);
7281
int carry = 0;
73-
ListNode dummy = new ListNode(-1);
7482
ListNode cur = dummy;
7583
while (l1 != null || l2 != null || carry != 0) {
7684
int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;

‎lcci/02.05.Sum Lists/Solution.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
*/
99
class Solution {
1010
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
11+
ListNode dummy = new ListNode(0);
1112
int carry = 0;
12-
ListNode dummy = new ListNode(-1);
1313
ListNode cur = dummy;
1414
while (l1 != null || l2 != null || carry != 0) {
1515
int s = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;

‎lcci/02.05.Sum Lists/Solution.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
class Solution:
99
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
10-
dummy = cur=ListNode(0)
11-
carry= 0
10+
dummy = ListNode()
11+
carry, curr= 0, dummy
1212
while l1 or l2 or carry:
13-
carry+= (0 if notl1 else l1.val) + (0 if notl2 else l2.val)
14-
cur.next=ListNode(carry% 10)
15-
cur = cur.next
16-
carry//=10
17-
l1 = None if notl1 else l1.next
18-
l2 = None if notl2 else l2.next
13+
s= (l1.val if l1 else 0) + (l2.val if l2 else 0) +carry
14+
carry, val=divmod(s, 10)
15+
curr.next = ListNode(val)
16+
curr=curr.next
17+
l1 = l1.next if l1 else None
18+
l2 = l2.next if l2 else None
1919
return dummy.next

‎solution/README.md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3014,7 +3014,7 @@
30143014
| 3001 | [捕获黑皇后需要的最少移动次数](/solution/3000-3099/3001.Minimum%20Moves%20to%20Capture%20The%20Queen/README.md) | `数组`,`枚举` | 中等 | 第 379 场周赛 |
30153015
| 3002 | [移除后集合的最多元素数](/solution/3000-3099/3002.Maximum%20Size%20of%20a%20Set%20After%20Removals/README.md) | `贪心`,`数组`,`哈希表` | 中等 | 第 379 场周赛 |
30163016
| 3003 | [执行操作后的最大分割数量](/solution/3000-3099/3003.Maximize%20the%20Number%20of%20Partitions%20After%20Operations/README.md) | `位运算`,`字符串`,`动态规划`,`状态压缩` | 困难 | 第 379 场周赛 |
3017-
| 3004 | [相同颜色的最大子树](/solution/3000-3099/3004.Maximum%20Subtree%20of%20the%20Same%20Color/README.md) | `树`,`深度优先搜索`,`数组` | 中等 | 🔒 |
3017+
| 3004 | [相同颜色的最大子树](/solution/3000-3099/3004.Maximum%20Subtree%20of%20the%20Same%20Color/README.md) | `树`,`深度优先搜索`,`数组`,`动态规划` | 中等 | 🔒 |
30183018
| 3005 | [最大频率元素计数](/solution/3000-3099/3005.Count%20Elements%20With%20Maximum%20Frequency/README.md) | `数组`,`哈希表`,`计数` | 简单 | 第 380 场周赛 |
30193019
| 3006 | [找出数组中的美丽下标 I](/solution/3000-3099/3006.Find%20Beautiful%20Indices%20in%20the%20Given%20Array%20I/README.md) | `双指针`,`字符串`,`二分查找`,`字符串匹配`,`哈希函数`,`滚动哈希` | 中等 | 第 380 场周赛 |
30203020
| 3007 | [价值和小于等于 K 的最大数字](/solution/3000-3099/3007.Maximum%20Number%20That%20Sum%20of%20the%20Prices%20Is%20Less%20Than%20or%20Equal%20to%20K/README.md) | `位运算`,`二分查找`,`动态规划` | 中等 | 第 380 场周赛 |
@@ -3033,15 +3033,15 @@
30333033
| 3020 | [子集中元素的最大数量](/solution/3000-3099/3020.Find%20the%20Maximum%20Number%20of%20Elements%20in%20Subset/README.md) | `数组`,`哈希表`,`枚举` | 中等 | 第 382 场周赛 |
30343034
| 3021 | [Alice 和 Bob 玩鲜花游戏](/solution/3000-3099/3021.Alice%20and%20Bob%20Playing%20Flower%20Game/README.md) | `数学` | 中等 | 第 382 场周赛 |
30353035
| 3022 | [给定操作次数内使剩余元素的或值最小](/solution/3000-3099/3022.Minimize%20OR%20of%20Remaining%20Elements%20Using%20Operations/README.md) | `贪心`,`位运算`,`数组` | 困难 | 第 382 场周赛 |
3036-
| 3023 | [在无限流中寻找模式 I](/solution/3000-3099/3023.Find%20Pattern%20in%20Infinite%20Stream%20I/README.md) | | 中等 | 🔒 |
3037-
| 3024 | [三角形类型 II](/solution/3000-3099/3024.Type%20of%20Triangle%20II/README.md) | | 简单 | 第 123 场双周赛 |
3038-
| 3025 | [人员站位的方案数 I](/solution/3000-3099/3025.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20I/README.md) | | 中等 | 第 123 场双周赛 |
3039-
| 3026 | [最大好子数组和](/solution/3000-3099/3026.Maximum%20Good%20Subarray%20Sum/README.md) | | 中等 | 第 123 场双周赛 |
3040-
| 3027 | [人员站位的方案数 II](/solution/3000-3099/3027.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20II/README.md) | | 困难 | 第 123 场双周赛 |
3041-
| 3028 | [边界上的蚂蚁](/solution/3000-3099/3028.Ant%20on%20the%20Boundary/README.md) | | 简单 | 第 383 场周赛 |
3042-
| 3029 | [将单词恢复初始状态所需的最短时间 I](/solution/3000-3099/3029.Minimum%20Time%20to%20Revert%20Word%20to%20Initial%20State%20I/README.md) | | 中等 | 第 383 场周赛 |
3043-
| 3030 | [找出网格的区域平均强度](/solution/3000-3099/3030.Find%20the%20Grid%20of%20Region%20Average/README.md) | | 中等 | 第 383 场周赛 |
3044-
| 3031 | [将单词恢复初始状态所需的最短时间 II](/solution/3000-3099/3031.Minimum%20Time%20to%20Revert%20Word%20to%20Initial%20State%20II/README.md) | | 困难 | 第 383 场周赛 |
3036+
| 3023 | [在无限流中寻找模式 I](/solution/3000-3099/3023.Find%20Pattern%20in%20Infinite%20Stream%20I/README.md) | `数组`,`字符串`,`字符串匹配`,`滑动窗口` | 中等 | 🔒 |
3037+
| 3024 | [三角形类型 II](/solution/3000-3099/3024.Type%20of%20Triangle%20II/README.md) | `数组`,`数学`,`排序` | 简单 | 第 123 场双周赛 |
3038+
| 3025 | [人员站位的方案数 I](/solution/3000-3099/3025.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20I/README.md) | `几何`,`数组`,`数学`,`枚举`,`排序` | 中等 | 第 123 场双周赛 |
3039+
| 3026 | [最大好子数组和](/solution/3000-3099/3026.Maximum%20Good%20Subarray%20Sum/README.md) | `数组`,`哈希表`,`前缀和` | 中等 | 第 123 场双周赛 |
3040+
| 3027 | [人员站位的方案数 II](/solution/3000-3099/3027.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20II/README.md) | `几何`,`数组`,`数学`,`枚举`,`排序` | 困难 | 第 123 场双周赛 |
3041+
| 3028 | [边界上的蚂蚁](/solution/3000-3099/3028.Ant%20on%20the%20Boundary/README.md) | `数组`,`前缀和`,`模拟` | 简单 | 第 383 场周赛 |
3042+
| 3029 | [将单词恢复初始状态所需的最短时间 I](/solution/3000-3099/3029.Minimum%20Time%20to%20Revert%20Word%20to%20Initial%20State%20I/README.md) | `字符串`,`字符串匹配`,`哈希函数`,`滚动哈希` | 中等 | 第 383 场周赛 |
3043+
| 3030 | [找出网格的区域平均强度](/solution/3000-3099/3030.Find%20the%20Grid%20of%20Region%20Average/README.md) | `数组`,`矩阵` | 中等 | 第 383 场周赛 |
3044+
| 3031 | [将单词恢复初始状态所需的最短时间 II](/solution/3000-3099/3031.Minimum%20Time%20to%20Revert%20Word%20to%20Initial%20State%20II/README.md) | `字符串`,`字符串匹配`,`哈希函数`,`滚动哈希` | 困难 | 第 383 场周赛 |
30453045

30463046
## 版权
30473047

‎solution/README_EN.md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3012,7 +3012,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
30123012
| 3001 | [Minimum Moves to Capture The Queen](/solution/3000-3099/3001.Minimum%20Moves%20to%20Capture%20The%20Queen/README_EN.md) | `Array`,`Enumeration` | Medium | Weekly Contest 379 |
30133013
| 3002 | [Maximum Size of a Set After Removals](/solution/3000-3099/3002.Maximum%20Size%20of%20a%20Set%20After%20Removals/README_EN.md) | `Greedy`,`Array`,`Hash Table` | Medium | Weekly Contest 379 |
30143014
| 3003 | [Maximize the Number of Partitions After Operations](/solution/3000-3099/3003.Maximize%20the%20Number%20of%20Partitions%20After%20Operations/README_EN.md) | `Bit Manipulation`,`String`,`Dynamic Programming`,`Bitmask` | Hard | Weekly Contest 379 |
3015-
| 3004 | [Maximum Subtree of the Same Color](/solution/3000-3099/3004.Maximum%20Subtree%20of%20the%20Same%20Color/README_EN.md) | `Tree`,`Depth-First Search`,`Array` | Medium | 🔒 |
3015+
| 3004 | [Maximum Subtree of the Same Color](/solution/3000-3099/3004.Maximum%20Subtree%20of%20the%20Same%20Color/README_EN.md) | `Tree`,`Depth-First Search`,`Array`,`Dynamic Programming` | Medium | 🔒 |
30163016
| 3005 | [Count Elements With Maximum Frequency](/solution/3000-3099/3005.Count%20Elements%20With%20Maximum%20Frequency/README_EN.md) | `Array`,`Hash Table`,`Counting` | Easy | Weekly Contest 380 |
30173017
| 3006 | [Find Beautiful Indices in the Given Array I](/solution/3000-3099/3006.Find%20Beautiful%20Indices%20in%20the%20Given%20Array%20I/README_EN.md) | `Two Pointers`,`String`,`Binary Search`,`String Matching`,`Hash Function`,`Rolling Hash` | Medium | Weekly Contest 380 |
30183018
| 3007 | [Maximum Number That Sum of the Prices Is Less Than or Equal to K](/solution/3000-3099/3007.Maximum%20Number%20That%20Sum%20of%20the%20Prices%20Is%20Less%20Than%20or%20Equal%20to%20K/README_EN.md) | `Bit Manipulation`,`Binary Search`,`Dynamic Programming` | Medium | Weekly Contest 380 |
@@ -3031,15 +3031,15 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
30313031
| 3020 | [Find the Maximum Number of Elements in Subset](/solution/3000-3099/3020.Find%20the%20Maximum%20Number%20of%20Elements%20in%20Subset/README_EN.md) | `Array`,`Hash Table`,`Enumeration` | Medium | Weekly Contest 382 |
30323032
| 3021 | [Alice and Bob Playing Flower Game](/solution/3000-3099/3021.Alice%20and%20Bob%20Playing%20Flower%20Game/README_EN.md) | `Math` | Medium | Weekly Contest 382 |
30333033
| 3022 | [Minimize OR of Remaining Elements Using Operations](/solution/3000-3099/3022.Minimize%20OR%20of%20Remaining%20Elements%20Using%20Operations/README_EN.md) | `Greedy`,`Bit Manipulation`,`Array` | Hard | Weekly Contest 382 |
3034-
| 3023 | [Find Pattern in Infinite Stream I](/solution/3000-3099/3023.Find%20Pattern%20in%20Infinite%20Stream%20I/README_EN.md) | | Medium | 🔒 |
3035-
| 3024 | [Type of Triangle II](/solution/3000-3099/3024.Type%20of%20Triangle%20II/README_EN.md) | | Easy | Biweekly Contest 123 |
3036-
| 3025 | [Find the Number of Ways to Place People I](/solution/3000-3099/3025.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20I/README_EN.md) | | Medium | Biweekly Contest 123 |
3037-
| 3026 | [Maximum Good Subarray Sum](/solution/3000-3099/3026.Maximum%20Good%20Subarray%20Sum/README_EN.md) | | Medium | Biweekly Contest 123 |
3038-
| 3027 | [Find the Number of Ways to Place People II](/solution/3000-3099/3027.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20II/README_EN.md) | | Hard | Biweekly Contest 123 |
3039-
| 3028 | [Ant on the Boundary](/solution/3000-3099/3028.Ant%20on%20the%20Boundary/README_EN.md) | | Easy | Weekly Contest 383 |
3040-
| 3029 | [Minimum Time to Revert Word to Initial State I](/solution/3000-3099/3029.Minimum%20Time%20to%20Revert%20Word%20to%20Initial%20State%20I/README_EN.md) | | Medium | Weekly Contest 383 |
3041-
| 3030 | [Find the Grid of Region Average](/solution/3000-3099/3030.Find%20the%20Grid%20of%20Region%20Average/README_EN.md) | | Medium | Weekly Contest 383 |
3042-
| 3031 | [Minimum Time to Revert Word to Initial State II](/solution/3000-3099/3031.Minimum%20Time%20to%20Revert%20Word%20to%20Initial%20State%20II/README_EN.md) | | Hard | Weekly Contest 383 |
3034+
| 3023 | [Find Pattern in Infinite Stream I](/solution/3000-3099/3023.Find%20Pattern%20in%20Infinite%20Stream%20I/README_EN.md) | `Array`,`String`,`String Matching`,`Sliding Window` | Medium | 🔒 |
3035+
| 3024 | [Type of Triangle II](/solution/3000-3099/3024.Type%20of%20Triangle%20II/README_EN.md) | `Array`,`Math`,`Sorting` | Easy | Biweekly Contest 123 |
3036+
| 3025 | [Find the Number of Ways to Place People I](/solution/3000-3099/3025.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20I/README_EN.md) | `Geometry`,`Array`,`Math`,`Enumeration`,`Sorting` | Medium | Biweekly Contest 123 |
3037+
| 3026 | [Maximum Good Subarray Sum](/solution/3000-3099/3026.Maximum%20Good%20Subarray%20Sum/README_EN.md) | `Array`,`Hash Table`,`Prefix Sum` | Medium | Biweekly Contest 123 |
3038+
| 3027 | [Find the Number of Ways to Place People II](/solution/3000-3099/3027.Find%20the%20Number%20of%20Ways%20to%20Place%20People%20II/README_EN.md) | `Geometry`,`Array`,`Math`,`Enumeration`,`Sorting` | Hard | Biweekly Contest 123 |
3039+
| 3028 | [Ant on the Boundary](/solution/3000-3099/3028.Ant%20on%20the%20Boundary/README_EN.md) | `Array`,`Prefix Sum`,`Simulation` | Easy | Weekly Contest 383 |
3040+
| 3029 | [Minimum Time to Revert Word to Initial State I](/solution/3000-3099/3029.Minimum%20Time%20to%20Revert%20Word%20to%20Initial%20State%20I/README_EN.md) | `String`,`String Matching`,`Hash Function`,`Rolling Hash` | Medium | Weekly Contest 383 |
3041+
| 3030 | [Find the Grid of Region Average](/solution/3000-3099/3030.Find%20the%20Grid%20of%20Region%20Average/README_EN.md) | `Array`,`Matrix` | Medium | Weekly Contest 383 |
3042+
| 3031 | [Minimum Time to Revert Word to Initial State II](/solution/3000-3099/3031.Minimum%20Time%20to%20Revert%20Word%20to%20Initial%20State%20II/README_EN.md) | `String`,`String Matching`,`Hash Function`,`Rolling Hash` | Hard | Weekly Contest 383 |
30433043

30443044
## Copyright
30453045

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /