Skip to content

Commit cb1676f

Browse files
committed
feat: update solutions to lc problem: No.0002
No.0002.Add Two Numbers
1 parent aaa5ab0 commit cb1676f

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

solution/0000-0099/0002.Add Two Numbers/README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:模拟**
54+
55+
同时遍历两个链表 $l1$, $l2$,对应节点值相加,进位记为 $carry$。当 $l1$, $l2$ 同时遍历结束,并且 $carry$ 为 $0$ 时,结束遍历。
56+
57+
时间复杂度 $O(max(m, n))$,其中 $m$, $n$ 分别表示两个链表的长度。忽略结果链表的空间消耗,空间复杂度 $O(1)$。
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**
@@ -63,16 +69,16 @@
6369
# self.val = val
6470
# self.next = next
6571
class Solution:
66-
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
72+
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
6773
dummy = ListNode()
68-
carry, cur = 0, dummy
74+
carry, curr = 0, dummy
6975
while l1 or l2 or carry:
70-
s = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
76+
s = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry
7177
carry, val = divmod(s, 10)
72-
cur.next = ListNode(val)
73-
cur = cur.next
74-
l1 = None if not l1 else l1.next
75-
l2 = None if not l2 else l2.next
78+
curr.next = ListNode(val)
79+
curr = curr.next
80+
l1 = l1.next if l1 else None
81+
l2 = l2.next if l2 else None
7682
return dummy.next
7783
```
7884

solution/0000-0099/0002.Add Two Numbers/README_EN.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@
5353
# self.val = val
5454
# self.next = next
5555
class Solution:
56-
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
56+
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
5757
dummy = ListNode()
58-
carry, cur = 0, dummy
58+
carry, curr = 0, dummy
5959
while l1 or l2 or carry:
60-
s = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
60+
s = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry
6161
carry, val = divmod(s, 10)
62-
cur.next = ListNode(val)
63-
cur = cur.next
64-
l1 = None if not l1 else l1.next
65-
l2 = None if not l2 else l2.next
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
6666
return dummy.next
6767
```
6868

solution/0000-0099/0002.Add Two Numbers/Solution.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
# self.val = val
55
# self.next = next
66
class Solution:
7-
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
7+
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
88
dummy = ListNode()
9-
carry, cur = 0, dummy
9+
carry, curr = 0, dummy
1010
while l1 or l2 or carry:
11-
s = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
11+
s = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry
1212
carry, val = divmod(s, 10)
13-
cur.next = ListNode(val)
14-
cur = cur.next
15-
l1 = None if not l1 else l1.next
16-
l2 = None if not l2 else l2.next
13+
curr.next = ListNode(val)
14+
curr = curr.next
15+
l1 = l1.next if l1 else None
16+
l2 = l2.next if l2 else None
1717
return dummy.next

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@
5050

5151
**方法一:双指针 + 哈希表**
5252

53-
定义一个哈希表记录当前窗口内出现的字符,i、j 分别表示不重复子串的开始位置和结束位置,ans 表示无重复字符子串的最大长度。
53+
定义一个哈希表记录当前窗口内出现的字符,$i$, $j$ 分别表示不重复子串的开始位置和结束位置,$ans$ 表示无重复字符子串的最大长度。
5454

55-
遍历 s 每个字符 c,若 `[i, j - 1]` 窗口内存在 `c`,则 i 循环向右移动,更新哈希表,直至 `[i, j - 1]` 窗口不存在 `c`,循环结束。将 `c` 加入哈希表中,此时 `[i, j]` 窗口内不含重复元素,更新 ans 的最大值:`ans = max(ans, j - i + 1)`
55+
遍历 $s$ 每个字符 $c$,若 $[i, j - 1]$ 窗口内存在 $c$,则 $i$ 循环向右移动,更新哈希表,直至 $[i, j - 1]$ 窗口不存在 $c$,循环结束。将 $c$ 加入哈希表中,此时 $[i, j]$ 窗口内不含重复元素,更新 $ans$ 的最大值:$ans = max(ans, j - i + 1)$
5656

57-
最后返回 ans 即可。
57+
最后返回 $ans$ 即可。
5858

59-
时间复杂度 O(n),其中 n 表示字符串 s 的长度。
59+
时间复杂度 $O(n)$,其中 $n$ 表示字符串 $s$ 的长度。
6060

6161
双指针算法模板:
6262

0 commit comments

Comments
 (0)