Skip to content

Commit 898db48

Browse files
committed
feat: update solutions to lc problems
* No.0067.Add Binary * No.0445.Add Two Numbers II * No.0591.Tag Validator
1 parent 6b390eb commit 898db48

File tree

9 files changed

+62
-44
lines changed

9 files changed

+62
-44
lines changed

solution/0000-0099/0067.Add Binary/README.md

+17-7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ class Solution:
4848
return bin(int(a, 2) + int(b, 2))[2:]
4949
```
5050

51+
```python
52+
class Solution:
53+
def addBinary(self, a: str, b: str) -> str:
54+
ans = []
55+
i, j, carry = len(a) - 1, len(b) - 1, 0
56+
while i >= 0 or j >= 0 or carry:
57+
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
58+
carry, v = divmod(carry, 2)
59+
ans.append(str(v))
60+
i, j = i - 1, j - 1
61+
return ''.join(ans[::-1])
62+
```
63+
5164
### **Java**
5265

5366
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -56,13 +69,10 @@ class Solution:
5669
class Solution {
5770
public String addBinary(String a, String b) {
5871
StringBuilder sb = new StringBuilder();
59-
int i = a.length() - 1, j = b.length() - 1, carry = 0;
60-
while (i >= 0 || j >= 0 || carry != 0) {
61-
int s = carry + (i >= 0 ? a.charAt(i) - '0' : 0) + (j >= 0 ? b.charAt(j) - '0' : 0);
62-
sb.append(s % 2);
63-
carry = s / 2;
64-
--i;
65-
--j;
72+
for (int i = a.length() - 1, j = b.length() - 1, carry = 0; i >= 0 || j >= 0 || carry > 0; --i, --j) {
73+
carry += (i >= 0 ? a.charAt(i) - '0' : 0) + (j >= 0 ? b.charAt(j) - '0' : 0);
74+
sb.append(carry % 2);
75+
carry /= 2;
6676
}
6777
return sb.reverse().toString();
6878
}

solution/0000-0099/0067.Add Binary/README_EN.md

+17-7
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,29 @@ class Solution:
3535
return bin(int(a, 2) + int(b, 2))[2:]
3636
```
3737

38+
```python
39+
class Solution:
40+
def addBinary(self, a: str, b: str) -> str:
41+
ans = []
42+
i, j, carry = len(a) - 1, len(b) - 1, 0
43+
while i >= 0 or j >= 0 or carry:
44+
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
45+
carry, v = divmod(carry, 2)
46+
ans.append(str(v))
47+
i, j = i - 1, j - 1
48+
return ''.join(ans[::-1])
49+
```
50+
3851
### **Java**
3952

4053
```java
4154
class Solution {
4255
public String addBinary(String a, String b) {
4356
StringBuilder sb = new StringBuilder();
44-
int i = a.length() - 1, j = b.length() - 1, carry = 0;
45-
while (i >= 0 || j >= 0 || carry != 0) {
46-
int s = carry + (i >= 0 ? a.charAt(i) - '0' : 0) + (j >= 0 ? b.charAt(j) - '0' : 0);
47-
sb.append(s % 2);
48-
carry = s / 2;
49-
--i;
50-
--j;
57+
for (int i = a.length() - 1, j = b.length() - 1, carry = 0; i >= 0 || j >= 0 || carry > 0; --i, --j) {
58+
carry += (i >= 0 ? a.charAt(i) - '0' : 0) + (j >= 0 ? b.charAt(j) - '0' : 0);
59+
sb.append(carry % 2);
60+
carry /= 2;
5161
}
5262
return sb.reverse().toString();
5363
}

solution/0000-0099/0067.Add Binary/Solution.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
class Solution {
22
public String addBinary(String a, String b) {
33
StringBuilder sb = new StringBuilder();
4-
int i = a.length() - 1, j = b.length() - 1, carry = 0;
5-
while (i >= 0 || j >= 0 || carry != 0) {
6-
int s = carry + (i >= 0 ? a.charAt(i) - '0' : 0) + (j >= 0 ? b.charAt(j) - '0' : 0);
7-
sb.append(s % 2);
8-
carry = s / 2;
9-
--i;
10-
--j;
4+
for (int i = a.length() - 1, j = b.length() - 1, carry = 0; i >= 0 || j >= 0 || carry > 0; --i, --j) {
5+
carry += (i >= 0 ? a.charAt(i) - '0' : 0) + (j >= 0 ? b.charAt(j) - '0' : 0);
6+
sb.append(carry % 2);
7+
carry /= 2;
118
}
129
return sb.reverse().toString();
1310
}

solution/0400-0499/0445.Add Two Numbers II/README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56-
利用栈将数字逆序。
56+
**方法一:栈**
57+
58+
利用两个栈分别存储两个链表元素。然后对两个栈中对应元素相加,并记录进位 carry。
5759

5860
<!-- tabs:start -->
5961

@@ -79,10 +81,9 @@ class Solution:
7981
carry, dummy = 0, ListNode()
8082
while s1 or s2 or carry:
8183
carry += (0 if not s1 else s1.pop()) + (0 if not s2 else s2.pop())
82-
# 创建结点,利用头插法将结点插入链表
83-
node = ListNode(carry % 10, dummy.next)
84+
carry, val = divmod(carry, 10)
85+
node = ListNode(val, dummy.next)
8486
dummy.next = node
85-
carry //= 10
8687
return dummy.next
8788
```
8889

@@ -102,6 +103,7 @@ class Solution:
102103
* }
103104
*/
104105
class Solution {
106+
105107
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
106108
Deque<Integer> s1 = new ArrayDeque<>();
107109
Deque<Integer> s2 = new ArrayDeque<>();
@@ -114,14 +116,16 @@ class Solution {
114116
int carry = 0;
115117
ListNode dummy = new ListNode();
116118
while (!s1.isEmpty() || !s2.isEmpty() || carry != 0) {
117-
carry += (s1.isEmpty() ? 0 : s1.pop()) + (s2.isEmpty() ? 0 : s2.pop());
119+
carry +=
120+
(s1.isEmpty() ? 0 : s1.pop()) + (s2.isEmpty() ? 0 : s2.pop());
118121
ListNode node = new ListNode(carry % 10, dummy.next);
119122
dummy.next = node;
120123
carry /= 10;
121124
}
122125
return dummy.next;
123126
}
124127
}
128+
125129
```
126130

127131
### **C++**

solution/0400-0499/0445.Add Two Numbers II/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class Solution:
6666
carry, dummy = 0, ListNode()
6767
while s1 or s2 or carry:
6868
carry += (0 if not s1 else s1.pop()) + (0 if not s2 else s2.pop())
69-
node = ListNode(carry % 10, dummy.next)
69+
carry, val = divmod(carry, 10)
70+
node = ListNode(val, dummy.next)
7071
dummy.next = node
71-
carry //= 10
7272
return dummy.next
7373
```
7474

solution/0400-0499/0445.Add Two Numbers II/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
1515
carry, dummy = 0, ListNode()
1616
while s1 or s2 or carry:
1717
carry += (0 if not s1 else s1.pop()) + (0 if not s2 else s2.pop())
18-
node = ListNode(carry % 10, dummy.next)
18+
carry, val = divmod(carry, 10)
19+
node = ListNode(val, dummy.next)
1920
dummy.next = node
20-
carry //= 10
2121
return dummy.next

solution/0500-0599/0591.Tag Validator/README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -107,33 +107,32 @@ cdata <strong>不</strong>是 <strong>&quot;&lt;![CDATA[&lt;div&gt;]&gt;]]&gt;]]
107107
class Solution:
108108
def isValid(self, code: str) -> bool:
109109
def check(tag):
110-
n = len(tag)
111-
return 1 <= n <= 9 and all(c.isupper() for c in tag)
110+
return 1 <= len(tag) <= 9 and all(c.isupper() for c in tag)
112111

113112
stk = []
114113
i, n = 0, len(code)
115114
while i < n:
116115
if i and not stk:
117116
return False
118-
if code[i: i + 9] == '<![CDATA[':
117+
if code[i : i + 9] == '<![CDATA[':
119118
i = code.find(']]>', i + 9)
120119
if i < 0:
121120
return False
122121
i += 2
123-
elif code[i: i + 2] == '</':
122+
elif code[i : i + 2] == '</':
124123
j = i + 2
125124
i = code.find('>', j)
126125
if i < 0:
127126
return False
128-
t = code[j: i]
127+
t = code[j:i]
129128
if not check(t) or not stk or stk.pop() != t:
130129
return False
131130
elif code[i] == '<':
132131
j = i + 1
133132
i = code.find('>', j)
134133
if i < 0:
135134
return False
136-
t = code[j: i]
135+
t = code[j:i]
137136
if not check(t):
138137
return False
139138
stk.append(t)

solution/0500-0599/0591.Tag Validator/README_EN.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -75,33 +75,32 @@ The reason why cdata is NOT <b>&quot;&lt;![CDATA[&lt;div&gt;]&gt;]]&gt;]]&gt;&qu
7575
class Solution:
7676
def isValid(self, code: str) -> bool:
7777
def check(tag):
78-
n = len(tag)
79-
return 1 <= n <= 9 and all(c.isupper() for c in tag)
78+
return 1 <= len(tag) <= 9 and all(c.isupper() for c in tag)
8079

8180
stk = []
8281
i, n = 0, len(code)
8382
while i < n:
8483
if i and not stk:
8584
return False
86-
if code[i: i + 9] == '<![CDATA[':
85+
if code[i : i + 9] == '<![CDATA[':
8786
i = code.find(']]>', i + 9)
8887
if i < 0:
8988
return False
9089
i += 2
91-
elif code[i: i + 2] == '</':
90+
elif code[i : i + 2] == '</':
9291
j = i + 2
9392
i = code.find('>', j)
9493
if i < 0:
9594
return False
96-
t = code[j: i]
95+
t = code[j:i]
9796
if not check(t) or not stk or stk.pop() != t:
9897
return False
9998
elif code[i] == '<':
10099
j = i + 1
101100
i = code.find('>', j)
102101
if i < 0:
103102
return False
104-
t = code[j: i]
103+
t = code[j:i]
105104
if not check(t):
106105
return False
107106
stk.append(t)

solution/0500-0599/0591.Tag Validator/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution:
22
def isValid(self, code: str) -> bool:
33
def check(tag):
4-
n = len(tag)
5-
return 1 <= n <= 9 and all(c.isupper() for c in tag)
4+
return 1 <= len(tag) <= 9 and all(c.isupper() for c in tag)
65

76
stk = []
87
i, n = 0, len(code)

0 commit comments

Comments
 (0)