Skip to content

Commit acb1e14

Browse files
committed
feat: add solutions to lcof2 problems: No.002,003
1 parent 372cb84 commit acb1e14

File tree

8 files changed

+204
-2
lines changed

8 files changed

+204
-2
lines changed

lcof2/剑指 Offer II 002. 二进制加法/README.md

+81-1
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,102 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44+
模拟笔算加法的过程,注意进位
45+
4446
<!-- tabs:start -->
4547

4648
### **Python3**
4749

4850
<!-- 这里可写当前语言的特殊实现逻辑 -->
4951

5052
```python
51-
53+
class Solution:
54+
def addBinary(self, a: str, b: str) -> str:
55+
x, y = len(a) - 1, len(b) - 1
56+
arr = []
57+
carry = 0
58+
while x >= 0 or y >= 0:
59+
if x >= 0:
60+
if a[x] == '1':
61+
carry += 1
62+
x -= 1
63+
if y >= 0:
64+
if b[y] == '1':
65+
carry += 1
66+
y -= 1
67+
arr.append(chr((carry & 1) + ord('0')))
68+
carry >>= 1
69+
if carry == 1:
70+
arr.append('1')
71+
return ''.join(reversed(arr))
5272
```
5373

5474
### **Java**
5575

5676
<!-- 这里可写当前语言的特殊实现逻辑 -->
5777

5878
```java
79+
class Solution {
80+
public String addBinary(String a, String b) {
81+
int x = a.length() - 1, y = b.length() - 1;
82+
StringBuilder builder = new StringBuilder();
83+
int carry = 0;
84+
while (x >= 0 || y >= 0) {
85+
if (x >= 0) {
86+
if (a.charAt(x) == '1') {
87+
carry += 1;
88+
}
89+
x--;
90+
}
91+
if (y >= 0) {
92+
if (b.charAt(y) == '1') {
93+
carry += 1;
94+
}
95+
y--;
96+
}
97+
builder.append((char) ((carry & 1) + '0'));
98+
carry >>= 1;
99+
}
100+
if (carry == 1) {
101+
builder.append('1');
102+
}
103+
return builder.reverse().toString();
104+
}
105+
}
106+
```
59107

108+
### **Go**
109+
110+
```go
111+
func addBinary(a string, b string) string {
112+
x, y := len(a)-1, len(b)-1
113+
var builder strings.Builder
114+
carry := 0
115+
for x >= 0 || y >= 0 {
116+
if x >= 0 {
117+
if a[x] == '1' {
118+
carry += 1
119+
}
120+
x--
121+
}
122+
if y >= 0 {
123+
if b[y] == '1' {
124+
carry += 1
125+
}
126+
y--
127+
}
128+
builder.WriteRune(rune(carry&1 + '0'))
129+
carry >>= 1
130+
}
131+
if carry == 1 {
132+
builder.WriteRune('1')
133+
}
134+
bytes := []byte(builder.String())
135+
for i, j := 0, len(bytes)-1; i < j; i, j = i+1, j-1 {
136+
bytes[i], bytes[j] = bytes[j], bytes[i]
137+
}
138+
return string(bytes)
139+
}
60140
```
61141

62142
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func addBinary(a string, b string) string {
2+
x, y := len(a)-1, len(b)-1
3+
var builder strings.Builder
4+
carry := 0
5+
for x >= 0 || y >= 0 {
6+
if x >= 0 {
7+
if a[x] == '1' {
8+
carry += 1
9+
}
10+
x--
11+
}
12+
if y >= 0 {
13+
if b[y] == '1' {
14+
carry += 1
15+
}
16+
y--
17+
}
18+
builder.WriteRune(rune(carry&1 + '0'))
19+
carry >>= 1
20+
}
21+
if carry == 1 {
22+
builder.WriteRune('1')
23+
}
24+
bytes := []byte(builder.String())
25+
for i, j := 0, len(bytes)-1; i < j; i, j = i+1, j-1 {
26+
bytes[i], bytes[j] = bytes[j], bytes[i]
27+
}
28+
return string(bytes)
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public String addBinary(String a, String b) {
3+
int x = a.length() - 1, y = b.length() - 1;
4+
StringBuilder builder = new StringBuilder();
5+
int carry = 0;
6+
while (x >= 0 || y >= 0) {
7+
if (x >= 0) {
8+
if (a.charAt(x) == '1') {
9+
carry += 1;
10+
}
11+
x--;
12+
}
13+
if (y >= 0) {
14+
if (b.charAt(y) == '1') {
15+
carry += 1;
16+
}
17+
y--;
18+
}
19+
builder.append((char) ((carry & 1) + '0'));
20+
carry >>= 1;
21+
}
22+
if (carry == 1) {
23+
builder.append('1');
24+
}
25+
return builder.reverse().toString();
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def addBinary(self, a: str, b: str) -> str:
3+
x, y = len(a) - 1, len(b) - 1
4+
arr = []
5+
carry = 0
6+
while x >= 0 or y >= 0:
7+
if x >= 0:
8+
if a[x] == '1':
9+
carry += 1
10+
x -= 1
11+
if y >= 0:
12+
if b[y] == '1':
13+
carry += 1
14+
y -= 1
15+
arr.append(chr((carry & 1) + ord('0')))
16+
carry >>= 1
17+
if carry == 1:
18+
arr.append('1')
19+
return ''.join(reversed(arr))

lcof2/剑指 Offer II 003. 前 n 个数字二进制中 1 的个数/README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,40 @@
6767
<!-- 这里可写当前语言的特殊实现逻辑 -->
6868

6969
```python
70-
70+
class Solution:
71+
def countBits(self, n: int) -> List[int]:
72+
dp = [0 for _ in range(n + 1)]
73+
for i in range(1, n + 1):
74+
dp[i] = dp[i & (i - 1)] + 1
75+
return dp
7176
```
7277

7378
### **Java**
7479

7580
<!-- 这里可写当前语言的特殊实现逻辑 -->
7681

7782
```java
83+
class Solution {
84+
public int[] countBits(int n) {
85+
int[] dp = new int[n + 1];
86+
for (int i = 1; i <= n; i++) {
87+
dp[i] = dp[i & (i - 1)] + 1;
88+
}
89+
return dp;
90+
}
91+
}
92+
```
93+
94+
### **Go**
7895

96+
```go
97+
func countBits(n int) []int {
98+
dp := make([]int, n+1)
99+
for i := 1; i <= n; i++ {
100+
dp[i] = dp[i&(i-1)] + 1
101+
}
102+
return dp
103+
}
79104
```
80105

81106
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func countBits(n int) []int {
2+
dp := make([]int, n+1)
3+
for i := 1; i <= n; i++ {
4+
dp[i] = dp[i&(i-1)] + 1
5+
}
6+
return dp
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int[] countBits(int n) {
3+
int[] dp = new int[n + 1];
4+
for (int i = 1; i <= n; i++) {
5+
dp[i] = dp[i & (i - 1)] + 1;
6+
}
7+
return dp;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def countBits(self, n: int) -> List[int]:
3+
dp = [0 for _ in range(n + 1)]
4+
for i in range(1, n + 1):
5+
dp[i] = dp[i & (i - 1)] + 1
6+
return dp

0 commit comments

Comments
 (0)