File tree 14 files changed +330
-0
lines changed
剑指 Offer II 003. 前 n 个数字二进制中 1 的个数
剑指 Offer II 005. 单词长度的最大乘积
剑指 Offer II 007. 数组中和为 0 的三个数
0000-0099/0067.Add Binary
0300-0399/0338.Counting Bits
14 files changed +330
-0
lines changed Original file line number Diff line number Diff line change @@ -139,6 +139,34 @@ func addBinary(a string, b string) string {
139
139
}
140
140
```
141
141
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
+
142
170
### **...**
143
171
144
172
```
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change @@ -103,6 +103,22 @@ func countBits(n int) []int {
103
103
}
104
104
```
105
105
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
+
106
122
### ** ...**
107
123
108
124
```
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change @@ -130,6 +130,40 @@ func max(a, b int) int {
130
130
}
131
131
```
132
132
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
+
133
167
### ** ...**
134
168
135
169
```
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change @@ -154,6 +154,42 @@ func threeSum(nums []int) [][]int {
154
154
}
155
155
```
156
156
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
+
157
193
### ** ...**
158
194
159
195
```
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change @@ -70,6 +70,36 @@ class Solution {
70
70
}
71
71
```
72
72
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
+
73
103
### **C#**
74
104
75
105
```cs
Original file line number Diff line number Diff line change @@ -55,6 +55,35 @@ class Solution {
55
55
}
56
56
```
57
57
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
+
58
87
### **C#**
59
88
60
89
```cs
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change 49
49
50
50
```
51
51
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
+
52
68
### ** ...**
53
69
54
70
```
Original file line number Diff line number Diff line change 65
65
66
66
```
67
67
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
+
68
84
### ** ...**
69
85
70
86
```
Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments