Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 20 additions & 46 deletions solution/0000-0099/0067.Add Binary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ tags:

### 方法一:模拟

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

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

<!-- tabs:start -->

Expand All @@ -64,7 +64,14 @@ tags:
```python
class Solution:
def addBinary(self, a: str, b: str) -> str:
return bin(int(a, 2) + int(b, 2))[2:]
ans = []
i, j, carry = len(a) - 1, len(b) - 1, 0
while i >= 0 or j >= 0 or carry:
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
carry, v = divmod(carry, 2)
ans.append(str(v))
i, j = i - 1, j - 1
return "".join(ans[::-1])
```

#### Java
Expand Down Expand Up @@ -130,7 +137,16 @@ func addBinary(a string, b string) string {

```ts
function addBinary(a: string, b: string): string {
return (BigInt('0b' + a) + BigInt('0b' + b)).toString(2);
let i = a.length - 1;
let j = b.length - 1;
const ans: number[] = [];
for (let carry = 0; i >= 0 || j >= 0 || carry; --i, --j) {
carry += (i >= 0 ? a[i] : '0').charCodeAt(0) - '0'.charCodeAt(0);
carry += (j >= 0 ? b[j] : '0').charCodeAt(0) - '0'.charCodeAt(0);
ans.push(carry % 2);
carry >>= 1;
}
return ans.reverse().join('');
}
```

Expand Down Expand Up @@ -187,46 +203,4 @@ public class Solution {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def addBinary(self, a: str, b: str) -> str:
ans = []
i, j, carry = len(a) - 1, len(b) - 1, 0
while i >= 0 or j >= 0 or carry:
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
carry, v = divmod(carry, 2)
ans.append(str(v))
i, j = i - 1, j - 1
return ''.join(ans[::-1])
```

#### TypeScript

```ts
function addBinary(a: string, b: string): string {
let i = a.length - 1;
let j = b.length - 1;
let ans: number[] = [];
for (let carry = 0; i >= 0 || j >= 0 || carry; --i, --j) {
carry += (i >= 0 ? a[i] : '0').charCodeAt(0) - '0'.charCodeAt(0);
carry += (j >= 0 ? b[j] : '0').charCodeAt(0) - '0'.charCodeAt(0);
ans.push(carry % 2);
carry >>= 1;
}
return ans.reverse().join('');
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
64 changes: 19 additions & 45 deletions solution/0000-0099/0067.Add Binary/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ tags:

### Solution 1: Simulation

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.
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.

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)$.

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

#### Java
Expand Down Expand Up @@ -123,7 +130,16 @@ func addBinary(a string, b string) string {

```ts
function addBinary(a: string, b: string): string {
return (BigInt('0b' + a) + BigInt('0b' + b)).toString(2);
let i = a.length - 1;
let j = b.length - 1;
const ans: number[] = [];
for (let carry = 0; i >= 0 || j >= 0 || carry; --i, --j) {
carry += (i >= 0 ? a[i] : '0').charCodeAt(0) - '0'.charCodeAt(0);
carry += (j >= 0 ? b[j] : '0').charCodeAt(0) - '0'.charCodeAt(0);
ans.push(carry % 2);
carry >>= 1;
}
return ans.reverse().join('');
}
```

Expand Down Expand Up @@ -180,46 +196,4 @@ public class Solution {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Python3

```python
class Solution:
def addBinary(self, a: str, b: str) -> str:
ans = []
i, j, carry = len(a) - 1, len(b) - 1, 0
while i >= 0 or j >= 0 or carry:
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
carry, v = divmod(carry, 2)
ans.append(str(v))
i, j = i - 1, j - 1
return ''.join(ans[::-1])
```

#### TypeScript

```ts
function addBinary(a: string, b: string): string {
let i = a.length - 1;
let j = b.length - 1;
let ans: number[] = [];
for (let carry = 0; i >= 0 || j >= 0 || carry; --i, --j) {
carry += (i >= 0 ? a[i] : '0').charCodeAt(0) - '0'.charCodeAt(0);
carry += (j >= 0 ? b[j] : '0').charCodeAt(0) - '0'.charCodeAt(0);
ans.push(carry % 2);
carry >>= 1;
}
return ans.reverse().join('');
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
9 changes: 8 additions & 1 deletion solution/0000-0099/0067.Add Binary/Solution.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
class Solution:
def addBinary(self, a: str, b: str) -> str:
return bin(int(a, 2) + int(b, 2))[2:]
ans = []
i, j, carry = len(a) - 1, len(b) - 1, 0
while i >= 0 or j >= 0 or carry:
carry += (0 if i < 0 else int(a[i])) + (0 if j < 0 else int(b[j]))
carry, v = divmod(carry, 2)
ans.append(str(v))
i, j = i - 1, j - 1
return "".join(ans[::-1])
11 changes: 10 additions & 1 deletion solution/0000-0099/0067.Add Binary/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
function addBinary(a: string, b: string): string {
return (BigInt('0b' + a) + BigInt('0b' + b)).toString(2);
let i = a.length - 1;
let j = b.length - 1;
const ans: number[] = [];
for (let carry = 0; i >= 0 || j >= 0 || carry; --i, --j) {
carry += (i >= 0 ? a[i] : '0').charCodeAt(0) - '0'.charCodeAt(0);
carry += (j >= 0 ? b[j] : '0').charCodeAt(0) - '0'.charCodeAt(0);
ans.push(carry % 2);
carry >>= 1;
}
return ans.reverse().join('');
}
10 changes: 0 additions & 10 deletions solution/0000-0099/0067.Add Binary/Solution2.py

This file was deleted.

12 changes: 0 additions & 12 deletions solution/0000-0099/0067.Add Binary/Solution2.ts

This file was deleted.

Loading