Skip to content

Commit f9f3f4f

Browse files
authored
feat: update solutions for lc No.0067 (#4944)
1 parent 7e78976 commit f9f3f4f

File tree

6 files changed

+57
-115
lines changed

6 files changed

+57
-115
lines changed

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

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ tags:
5353

5454
### 方法一:模拟
5555

56-
我们用一个变量 $carry$ 记录当前的进位,用两个指针 $i$ 和 $j$ 分别指向 $a$ 和 $b$ 的末尾,从末尾到开头逐位相加即可。
56+
我们用一个变量 $\textit{carry}$ 记录当前的进位,用两个指针 $i$ 和 $j$ 分别指向 $a$ 和 $b$ 的末尾,从末尾到开头逐位相加即可。
5757

58-
时间复杂度 $O(\max(m, n))$,其中 $m$ 和 $n$ 分别为字符串 $a$ 和 $b$ 的长度。空间复杂度 $O(1)$。
58+
时间复杂度 $O(\max(m, n))$,其中 $m$ 和 $n$ 分别为字符串 $a$ 和 $b$ 的长度。空间复杂度 $O(\max(m, n))$。
5959

6060
<!-- tabs:start -->
6161

@@ -64,7 +64,14 @@ tags:
6464
```python
6565
class Solution:
6666
def addBinary(self, a: str, b: str) -> str:
67-
return bin(int(a, 2) + int(b, 2))[2:]
67+
ans = []
68+
i, j, carry = len(a) - 1, len(b) - 1, 0
69+
while i >= 0 or j >= 0 or carry:
70+
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
71+
carry, v = divmod(carry, 2)
72+
ans.append(str(v))
73+
i, j = i - 1, j - 1
74+
return "".join(ans[::-1])
6875
```
6976

7077
#### Java
@@ -130,7 +137,16 @@ func addBinary(a string, b string) string {
130137

131138
```ts
132139
function addBinary(a: string, b: string): string {
133-
return (BigInt('0b' + a) + BigInt('0b' + b)).toString(2);
140+
let i = a.length - 1;
141+
let j = b.length - 1;
142+
const ans: number[] = [];
143+
for (let carry = 0; i >= 0 || j >= 0 || carry; --i, --j) {
144+
carry += (i >= 0 ? a[i] : '0').charCodeAt(0) - '0'.charCodeAt(0);
145+
carry += (j >= 0 ? b[j] : '0').charCodeAt(0) - '0'.charCodeAt(0);
146+
ans.push(carry % 2);
147+
carry >>= 1;
148+
}
149+
return ans.reverse().join('');
134150
}
135151
```
136152

@@ -187,46 +203,4 @@ public class Solution {
187203

188204
<!-- solution:end -->
189205

190-
<!-- solution:start -->
191-
192-
### 方法二
193-
194-
<!-- tabs:start -->
195-
196-
#### Python3
197-
198-
```python
199-
class Solution:
200-
def addBinary(self, a: str, b: str) -> str:
201-
ans = []
202-
i, j, carry = len(a) - 1, len(b) - 1, 0
203-
while i >= 0 or j >= 0 or carry:
204-
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
205-
carry, v = divmod(carry, 2)
206-
ans.append(str(v))
207-
i, j = i - 1, j - 1
208-
return ''.join(ans[::-1])
209-
```
210-
211-
#### TypeScript
212-
213-
```ts
214-
function addBinary(a: string, b: string): string {
215-
let i = a.length - 1;
216-
let j = b.length - 1;
217-
let ans: number[] = [];
218-
for (let carry = 0; i >= 0 || j >= 0 || carry; --i, --j) {
219-
carry += (i >= 0 ? a[i] : '0').charCodeAt(0) - '0'.charCodeAt(0);
220-
carry += (j >= 0 ? b[j] : '0').charCodeAt(0) - '0'.charCodeAt(0);
221-
ans.push(carry % 2);
222-
carry >>= 1;
223-
}
224-
return ans.reverse().join('');
225-
}
226-
```
227-
228-
<!-- tabs:end -->
229-
230-
<!-- solution:end -->
231-
232206
<!-- problem:end -->

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

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ tags:
4646

4747
### Solution 1: Simulation
4848

49-
We use a variable $carry$ to record the current carry, and two pointers $i$ and $j$ to point to the end of $a$ and $b$ respectively, and add them bit by bit from the end to the beginning.
49+
We use a variable $\textit{carry}$ to record the current carry, and two pointers $i$ and $j$ to point to the end of $a$ and $b$ respectively, and add them bit by bit from the end to the beginning.
5050

5151
The time complexity is $O(\max(m, n))$, where $m$ and $n$ are the lengths of strings $a$ and $b$ respectively. The space complexity is $O(1)$.
5252

@@ -57,7 +57,14 @@ The time complexity is $O(\max(m, n))$, where $m$ and $n$ are the lengths of str
5757
```python
5858
class Solution:
5959
def addBinary(self, a: str, b: str) -> str:
60-
return bin(int(a, 2) + int(b, 2))[2:]
60+
ans = []
61+
i, j, carry = len(a) - 1, len(b) - 1, 0
62+
while i >= 0 or j >= 0 or carry:
63+
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
64+
carry, v = divmod(carry, 2)
65+
ans.append(str(v))
66+
i, j = i - 1, j - 1
67+
return "".join(ans[::-1])
6168
```
6269

6370
#### Java
@@ -123,7 +130,16 @@ func addBinary(a string, b string) string {
123130

124131
```ts
125132
function addBinary(a: string, b: string): string {
126-
return (BigInt('0b' + a) + BigInt('0b' + b)).toString(2);
133+
let i = a.length - 1;
134+
let j = b.length - 1;
135+
const ans: number[] = [];
136+
for (let carry = 0; i >= 0 || j >= 0 || carry; --i, --j) {
137+
carry += (i >= 0 ? a[i] : '0').charCodeAt(0) - '0'.charCodeAt(0);
138+
carry += (j >= 0 ? b[j] : '0').charCodeAt(0) - '0'.charCodeAt(0);
139+
ans.push(carry % 2);
140+
carry >>= 1;
141+
}
142+
return ans.reverse().join('');
127143
}
128144
```
129145

@@ -180,46 +196,4 @@ public class Solution {
180196

181197
<!-- solution:end -->
182198

183-
<!-- solution:start -->
184-
185-
### Solution 2
186-
187-
<!-- tabs:start -->
188-
189-
#### Python3
190-
191-
```python
192-
class Solution:
193-
def addBinary(self, a: str, b: str) -> str:
194-
ans = []
195-
i, j, carry = len(a) - 1, len(b) - 1, 0
196-
while i >= 0 or j >= 0 or carry:
197-
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
198-
carry, v = divmod(carry, 2)
199-
ans.append(str(v))
200-
i, j = i - 1, j - 1
201-
return ''.join(ans[::-1])
202-
```
203-
204-
#### TypeScript
205-
206-
```ts
207-
function addBinary(a: string, b: string): string {
208-
let i = a.length - 1;
209-
let j = b.length - 1;
210-
let ans: number[] = [];
211-
for (let carry = 0; i >= 0 || j >= 0 || carry; --i, --j) {
212-
carry += (i >= 0 ? a[i] : '0').charCodeAt(0) - '0'.charCodeAt(0);
213-
carry += (j >= 0 ? b[j] : '0').charCodeAt(0) - '0'.charCodeAt(0);
214-
ans.push(carry % 2);
215-
carry >>= 1;
216-
}
217-
return ans.reverse().join('');
218-
}
219-
```
220-
221-
<!-- tabs:end -->
222-
223-
<!-- solution:end -->
224-
225199
<!-- problem:end -->
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
class Solution:
22
def addBinary(self, a: str, b: str) -> str:
3-
return bin(int(a, 2) + int(b, 2))[2:]
3+
ans = []
4+
i, j, carry = len(a) - 1, len(b) - 1, 0
5+
while i >= 0 or j >= 0 or carry:
6+
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
7+
carry, v = divmod(carry, 2)
8+
ans.append(str(v))
9+
i, j = i - 1, j - 1
10+
return "".join(ans[::-1])
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
function addBinary(a: string, b: string): string {
2-
return (BigInt('0b' + a) + BigInt('0b' + b)).toString(2);
2+
let i = a.length - 1;
3+
let j = b.length - 1;
4+
const ans: number[] = [];
5+
for (let carry = 0; i >= 0 || j >= 0 || carry; --i, --j) {
6+
carry += (i >= 0 ? a[i] : '0').charCodeAt(0) - '0'.charCodeAt(0);
7+
carry += (j >= 0 ? b[j] : '0').charCodeAt(0) - '0'.charCodeAt(0);
8+
ans.push(carry % 2);
9+
carry >>= 1;
10+
}
11+
return ans.reverse().join('');
312
}

solution/0000-0099/0067.Add Binary/Solution2.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

solution/0000-0099/0067.Add Binary/Solution2.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)