Skip to content

Commit 86a669a

Browse files
authored
feat: add solutions to lc/lcof2 problems (#629)
* feat: add solutions to lc/lcof2 problems * feat: add solutions to lc/lcof2 problems * feat: update README.md
1 parent 7bafa43 commit 86a669a

File tree

14 files changed

+330
-0
lines changed

14 files changed

+330
-0
lines changed

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

+28
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,34 @@ func addBinary(a string, b string) string {
139139
}
140140
```
141141

142+
### **C++**
143+
144+
```cpp
145+
class Solution {
146+
public:
147+
string addBinary(string a, string b) {
148+
string res;
149+
int carry = 0;
150+
151+
int i = a.size() - 1;
152+
int j = b.size() - 1;
153+
154+
while (i >= 0 || j >= 0) {
155+
int digitA = i >= 0 ? a.at(i--) - '0' : 0;
156+
int digitB = j >= 0 ? b.at(j--) - '0' : 0;
157+
int sum = digitA + digitB + carry;
158+
carry = sum >= 2 ? 1 : 0;
159+
sum = sum >= 2 ? sum - 2 : sum;
160+
res += to_string(sum);
161+
}
162+
163+
if (carry == 1) res.push_back('1');
164+
reverse(res.begin(), res.end());
165+
return res;
166+
}
167+
};
168+
```
169+
142170
### **...**
143171
144172
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
string addBinary(string a, string b) {
4+
string res;
5+
int carry = 0;
6+
7+
int i = a.size() - 1;
8+
int j = b.size() - 1;
9+
10+
while (i >= 0 || j >= 0) {
11+
int digitA = i >= 0 ? a.at(i--) - '0' : 0;
12+
int digitB = j >= 0 ? b.at(j--) - '0' : 0;
13+
int sum = digitA + digitB + carry;
14+
carry = sum >= 2 ? 1 : 0;
15+
sum = sum >= 2 ? sum - 2 : sum;
16+
res += to_string(sum);
17+
}
18+
19+
if (carry == 1) res.push_back('1');
20+
reverse(res.begin(), res.end());
21+
return res;
22+
}
23+
};

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

+16
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ func countBits(n int) []int {
103103
}
104104
```
105105

106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
vector<int> countBits(int n) {
112+
vector<int> res(n + 1);
113+
for (int i = 1; i <= n; i++) {
114+
res[i] = res[i & (i - 1)] + 1;
115+
}
116+
117+
return res;
118+
}
119+
};
120+
```
121+
106122
### **...**
107123

108124
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
vector<int> countBits(int n) {
4+
vector<int> res(n + 1);
5+
for (int i = 1; i <= n; i++) {
6+
res[i] = res[i & (i - 1)] + 1;
7+
}
8+
9+
return res;
10+
}
11+
};

lcof2/剑指 Offer II 005. 单词长度的最大乘积/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,40 @@ func max(a, b int) int {
130130
}
131131
```
132132

133+
134+
### **C++**
135+
136+
```cpp
137+
class Solution {
138+
public:
139+
int maxProduct(vector<string>& words) {
140+
vector<vector<bool>> hash(words.size(), vector<bool>(26, false));
141+
for (int i = 0; i < words.size(); i++)
142+
for (char c: words[i])
143+
hash[i][c - 'a'] = true;
144+
145+
int res = 0;
146+
for (int i = 0; i < words.size(); i++) {
147+
for (int j = i + 1; j < words.size(); j++) {
148+
int k = 0;
149+
for (; k < 26; k++) {
150+
if (hash[i][k] && hash[j][k])
151+
break;
152+
}
153+
154+
if (k == 26) {
155+
int prod = words[i].size() * words[j].size();
156+
res = max(res, prod);
157+
}
158+
}
159+
}
160+
161+
return res;
162+
}
163+
};
164+
```
165+
166+
133167
### **...**
134168

135169
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int maxProduct(vector<string>& words) {
4+
vector<vector<bool>> hash(words.size(), vector<bool>(26, false));
5+
for (int i = 0; i < words.size(); i++)
6+
for (char c: words[i])
7+
hash[i][c - 'a'] = true;
8+
9+
int res = 0;
10+
for (int i = 0; i < words.size(); i++) {
11+
for (int j = i + 1; j < words.size(); j++) {
12+
int k = 0;
13+
for (; k < 26; k++) {
14+
if (hash[i][k] && hash[j][k])
15+
break;
16+
}
17+
18+
if (k == 26) {
19+
int prod = words[i].size() * words[j].size();
20+
res = max(res, prod);
21+
}
22+
}
23+
}
24+
25+
return res;
26+
}
27+
};

lcof2/剑指 Offer II 007. 数组中和为 0 的三个数/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,42 @@ func threeSum(nums []int) [][]int {
154154
}
155155
```
156156

157+
### **C++**
158+
159+
```cpp
160+
class Solution {
161+
public:
162+
vector<vector<int>> threeSum(vector<int>& nums) {
163+
vector<vector<int>> res;
164+
165+
sort(nums.begin(), nums.end());
166+
for (int k = 0; k < nums.size(); k++) {
167+
int i = k + 1;
168+
int j = nums.size() - 1;
169+
if (k > 0 && nums[k] == nums[k - 1]) continue;
170+
171+
while(i < j) {
172+
if (nums[i] + nums[j] + nums[k] == 0) {
173+
res.push_back(vector<int>{nums[k], nums[i], nums[j]});
174+
i++;
175+
j--;
176+
177+
while(i < j && nums[i] == nums[i - 1]) i++;
178+
while(i < j && nums[j] == nums[j + 1]) j--;
179+
} else if (nums[i] + nums[j] + nums[k] < 0) {
180+
i++;
181+
} else {
182+
j--;
183+
}
184+
}
185+
}
186+
187+
return res;
188+
}
189+
};
190+
```
191+
192+
157193
### **...**
158194

159195
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> threeSum(vector<int>& nums) {
4+
vector<vector<int>> res;
5+
6+
sort(nums.begin(), nums.end());
7+
for (int k = 0; k < nums.size(); k++) {
8+
int i = k + 1;
9+
int j = nums.size() - 1;
10+
if (k > 0 && nums[k] == nums[k - 1]) continue;
11+
12+
while(i < j) {
13+
if (nums[i] + nums[j] + nums[k] == 0) {
14+
res.push_back(vector<int>{nums[k], nums[i], nums[j]});
15+
i++;
16+
j--;
17+
18+
while(i < j && nums[i] == nums[i - 1]) i++;
19+
while(i < j && nums[j] == nums[j + 1]) j--;
20+
} else if (nums[i] + nums[j] + nums[k] < 0) {
21+
i++;
22+
} else {
23+
j--;
24+
}
25+
}
26+
}
27+
28+
return res;
29+
}
30+
};

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

+30
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,36 @@ class Solution {
7070
}
7171
```
7272

73+
### **C++**
74+
75+
```cpp
76+
class Solution {
77+
public:
78+
string addBinary(string a, string b) {
79+
string res;
80+
int carry = 0;
81+
82+
int i = a.size() - 1;
83+
int j = b.size() - 1;
84+
85+
while (i >= 0 || j >= 0) {
86+
int digitA = i >= 0 ? a.at(i--) - '0' : 0;
87+
int digitB = j >= 0 ? b.at(j--) - '0' : 0;
88+
int sum = digitA + digitB + carry;
89+
carry = sum >= 2 ? 1 : 0;
90+
sum = sum >= 2 ? sum - 2 : sum;
91+
res += to_string(sum);
92+
}
93+
94+
if (carry == 1) res.push_back('1');
95+
reverse(res.begin(), res.end());
96+
return res;
97+
}
98+
};
99+
```
100+
101+
102+
73103
### **C#**
74104
75105
```cs

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

+29
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,35 @@ class Solution {
5555
}
5656
```
5757

58+
### **C++**
59+
60+
```cpp
61+
class Solution {
62+
public:
63+
string addBinary(string a, string b) {
64+
string res;
65+
int carry = 0;
66+
67+
int i = a.size() - 1;
68+
int j = b.size() - 1;
69+
70+
while (i >= 0 || j >= 0) {
71+
int digitA = i >= 0 ? a.at(i--) - '0' : 0;
72+
int digitB = j >= 0 ? b.at(j--) - '0' : 0;
73+
int sum = digitA + digitB + carry;
74+
carry = sum >= 2 ? 1 : 0;
75+
sum = sum >= 2 ? sum - 2 : sum;
76+
res += to_string(sum);
77+
}
78+
79+
if (carry == 1) res.push_back('1');
80+
reverse(res.begin(), res.end());
81+
return res;
82+
}
83+
};
84+
```
85+
86+
5887
### **C#**
5988
6089
```cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
string addBinary(string a, string b) {
4+
string res;
5+
int carry = 0;
6+
7+
int i = a.size() - 1;
8+
int j = b.size() - 1;
9+
10+
while (i >= 0 || j >= 0) {
11+
int digitA = i >= 0 ? a.at(i--) - '0' : 0;
12+
int digitB = j >= 0 ? b.at(j--) - '0' : 0;
13+
int sum = digitA + digitB + carry;
14+
carry = sum >= 2 ? 1 : 0;
15+
sum = sum >= 2 ? sum - 2 : sum;
16+
res += to_string(sum);
17+
}
18+
19+
if (carry == 1) res.push_back('1');
20+
reverse(res.begin(), res.end());
21+
return res;
22+
}
23+
};

solution/0300-0399/0338.Counting Bits/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@
4949

5050
```
5151

52+
### **C++**
53+
54+
```cpp
55+
class Solution {
56+
public:
57+
vector<int> countBits(int n) {
58+
vector<int> res(n + 1);
59+
for (int i = 1; i <= n; i++) {
60+
res[i] = res[i & (i - 1)] + 1;
61+
}
62+
63+
return res;
64+
}
65+
};
66+
```
67+
5268
### **...**
5369

5470
```

solution/0300-0399/0338.Counting Bits/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@
6565

6666
```
6767

68+
### **C++**
69+
70+
```cpp
71+
class Solution {
72+
public:
73+
vector<int> countBits(int n) {
74+
vector<int> res(n + 1);
75+
for (int i = 1; i <= n; i++) {
76+
res[i] = res[i & (i - 1)] + 1;
77+
}
78+
79+
return res;
80+
}
81+
};
82+
```
83+
6884
### **...**
6985

7086
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
vector<int> countBits(int n) {
4+
vector<int> res(n + 1);
5+
for (int i = 1; i <= n; i++) {
6+
res[i] = res[i & (i - 1)] + 1;
7+
}
8+
9+
return res;
10+
}
11+
};

0 commit comments

Comments
 (0)