Skip to content

Commit f1966da

Browse files
committed
feat: add solutions to lc problems: No.0067,0989
- No.0067.Add Binary - No.0989.Add to Array-Form of Integer
1 parent b903be5 commit f1966da

File tree

7 files changed

+189
-90
lines changed

7 files changed

+189
-90
lines changed

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

+30-30
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,23 @@ func addBinary(a string, b string) string {
164164

165165
```ts
166166
function addBinary(a: string, b: string): string {
167+
const n = Math.max(a.length, b.length);
167168
const res = [];
168-
let i = a.length - 1;
169-
let j = b.length - 1;
170169
let isOver = false;
171-
while (i >= 0 || j >= 0 || isOver) {
172-
let sum = Number(a[i] || 0) + Number(b[j] || 0) + (isOver ? 1 : 0);
170+
for (let i = 0; i < n || isOver; i++) {
171+
let val = isOver ? 1 : 0;
173172
isOver = false;
174-
if (sum > 1) {
173+
if (a[a.length - i - 1] === '1') {
174+
val++;
175+
}
176+
if (b[b.length - i - 1] === '1') {
177+
val++;
178+
}
179+
if (val > 1) {
175180
isOver = true;
176-
sum -= 2;
181+
val -= 2;
177182
}
178-
res.push(sum);
179-
i--;
180-
j--;
183+
res.push(val);
181184
}
182185
return res.reverse().join('');
183186
}
@@ -188,31 +191,28 @@ function addBinary(a: string, b: string): string {
188191
```rust
189192
impl Solution {
190193
pub fn add_binary(a: String, b: String) -> String {
191-
let mut res = String::new();
192-
let a = a.as_bytes();
193-
let b = b.as_bytes();
194-
let mut i = a.len();
195-
let mut j = b.len();
194+
let n = a.len().max(b.len());
195+
let (a, b) = (a.as_bytes(), b.as_bytes());
196+
let mut res = vec![];
196197
let mut is_over = false;
197-
while i != 0 || j != 0 || is_over {
198-
let mut sum = if is_over { b'1' } else { b'0' };
199-
if i != 0 {
200-
sum += a[i - 1] - b'0';
201-
i -= 1;
198+
let mut i = 0;
199+
while i < n || is_over {
200+
let mut val = if is_over { 1 } else { 0 };
201+
is_over = false;
202+
if a.get(a.len() - i - 1).unwrap_or(&b'0') == &b'1' {
203+
val += 1;
204+
}
205+
if b.get(b.len() - i - 1).unwrap_or(&b'0') == &b'1' {
206+
val += 1;
202207
}
203-
if j != 0 {
204-
sum += b[j - 1] - b'0';
205-
j -= 1;
208+
if val > 1 {
209+
is_over = true;
210+
val -= 2;
206211
}
207-
is_over = if sum > b'1' {
208-
sum -= b'2' - b'0';
209-
true
210-
} else {
211-
false
212-
};
213-
res.push(char::from(sum));
212+
i += 1;
213+
res.push(char::from(b'0' + val));
214214
}
215-
res.chars().rev().collect()
215+
res.iter().rev().collect()
216216
}
217217
}
218218
```

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

+30-30
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,23 @@ func addBinary(a string, b string) string {
149149

150150
```ts
151151
function addBinary(a: string, b: string): string {
152+
const n = Math.max(a.length, b.length);
152153
const res = [];
153-
let i = a.length - 1;
154-
let j = b.length - 1;
155154
let isOver = false;
156-
while (i >= 0 || j >= 0 || isOver) {
157-
let sum = Number(a[i] || 0) + Number(b[j] || 0) + (isOver ? 1 : 0);
155+
for (let i = 0; i < n || isOver; i++) {
156+
let val = isOver ? 1 : 0;
158157
isOver = false;
159-
if (sum > 1) {
158+
if (a[a.length - i - 1] === '1') {
159+
val++;
160+
}
161+
if (b[b.length - i - 1] === '1') {
162+
val++;
163+
}
164+
if (val > 1) {
160165
isOver = true;
161-
sum -= 2;
166+
val -= 2;
162167
}
163-
res.push(sum);
164-
i--;
165-
j--;
168+
res.push(val);
166169
}
167170
return res.reverse().join('');
168171
}
@@ -173,31 +176,28 @@ function addBinary(a: string, b: string): string {
173176
```rust
174177
impl Solution {
175178
pub fn add_binary(a: String, b: String) -> String {
176-
let mut res = String::new();
177-
let a = a.as_bytes();
178-
let b = b.as_bytes();
179-
let mut i = a.len();
180-
let mut j = b.len();
179+
let n = a.len().max(b.len());
180+
let (a, b) = (a.as_bytes(), b.as_bytes());
181+
let mut res = vec![];
181182
let mut is_over = false;
182-
while i != 0 || j != 0 || is_over {
183-
let mut sum = if is_over { b'1' } else { b'0' };
184-
if i != 0 {
185-
sum += a[i - 1] - b'0';
186-
i -= 1;
183+
let mut i = 0;
184+
while i < n || is_over {
185+
let mut val = if is_over { 1 } else { 0 };
186+
is_over = false;
187+
if a.get(a.len() - i - 1).unwrap_or(&b'0') == &b'1' {
188+
val += 1;
189+
}
190+
if b.get(b.len() - i - 1).unwrap_or(&b'0') == &b'1' {
191+
val += 1;
187192
}
188-
if j != 0 {
189-
sum += b[j - 1] - b'0';
190-
j -= 1;
193+
if val > 1 {
194+
is_over = true;
195+
val -= 2;
191196
}
192-
is_over = if sum > b'1' {
193-
sum -= b'2' - b'0';
194-
true
195-
} else {
196-
false
197-
};
198-
res.push(char::from(sum));
197+
i += 1;
198+
res.push(char::from(b'0' + val));
199199
}
200-
res.chars().rev().collect()
200+
res.iter().rev().collect()
201201
}
202202
}
203203
```
+18-21
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
impl Solution {
22
pub fn add_binary(a: String, b: String) -> String {
3-
let mut res = String::new();
4-
let a = a.as_bytes();
5-
let b = b.as_bytes();
6-
let mut i = a.len();
7-
let mut j = b.len();
3+
let n = a.len().max(b.len());
4+
let (a, b) = (a.as_bytes(), b.as_bytes());
5+
let mut res = vec![];
86
let mut is_over = false;
9-
while i != 0 || j != 0 || is_over {
10-
let mut sum = if is_over { b'1' } else { b'0' };
11-
if i != 0 {
12-
sum += a[i - 1] - b'0';
13-
i -= 1;
7+
let mut i = 0;
8+
while i < n || is_over {
9+
let mut val = if is_over { 1 } else { 0 };
10+
is_over = false;
11+
if a.get(a.len() - i - 1).unwrap_or(&b'0') == &b'1' {
12+
val += 1;
1413
}
15-
if j != 0 {
16-
sum += b[j - 1] - b'0';
17-
j -= 1;
14+
if b.get(b.len() - i - 1).unwrap_or(&b'0') == &b'1' {
15+
val += 1;
1816
}
19-
is_over = if sum > b'1' {
20-
sum -= b'2' - b'0';
21-
true
22-
} else {
23-
false
24-
};
25-
res.push(char::from(sum));
17+
if val > 1 {
18+
is_over = true;
19+
val -= 2;
20+
}
21+
i += 1;
22+
res.push(char::from(b'0' + val));
2623
}
27-
res.chars().rev().collect()
24+
res.iter().rev().collect()
2825
}
2926
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
function addBinary(a: string, b: string): string {
2+
const n = Math.max(a.length, b.length);
23
const res = [];
3-
let i = a.length - 1;
4-
let j = b.length - 1;
54
let isOver = false;
6-
while (i >= 0 || j >= 0 || isOver) {
7-
let sum = Number(a[i] || 0) + Number(b[j] || 0) + (isOver ? 1 : 0);
5+
for (let i = 0; i < n || isOver; i++) {
6+
let val = isOver ? 1 : 0;
87
isOver = false;
9-
if (sum > 1) {
8+
if (a[a.length - i - 1] === '1') {
9+
val++;
10+
}
11+
if (b[b.length - i - 1] === '1') {
12+
val++;
13+
}
14+
if (val > 1) {
1015
isOver = true;
11-
sum -= 2;
16+
val -= 2;
1217
}
13-
res.push(sum);
14-
i--;
15-
j--;
18+
res.push(val);
1619
}
1720
return res.reverse().join('');
1821
}

solution/0900-0999/0989.Add to Array-Form of Integer/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,46 @@ function addToArrayForm(num: number[], k: number): number[] {
121121
}
122122
```
123123

124+
```ts
125+
function addToArrayForm(num: number[], k: number): number[] {
126+
const n = num.length;
127+
const res = [];
128+
let sum = 0;
129+
for (let i = 0; i < n || sum !== 0 || k !== 0; i++) {
130+
sum += num[n - i - 1] ?? 0;
131+
sum += k % 10;
132+
res.push(sum % 10);
133+
k = Math.floor(k / 10);
134+
sum = Math.floor(sum / 10);
135+
}
136+
return res.reverse();
137+
}
138+
```
139+
140+
### **Rust**
141+
142+
```rust
143+
impl Solution {
144+
pub fn add_to_array_form(num: Vec<i32>, mut k: i32) -> Vec<i32> {
145+
let n = num.len();
146+
let mut res = vec![];
147+
let mut i = 0;
148+
let mut sum = 0;
149+
while i < n || sum != 0 || k != 0 {
150+
sum += num.get(n - i - 1).unwrap_or(&0);
151+
sum += k % 10;
152+
res.push(sum % 10);
153+
154+
i += 1;
155+
k /= 10;
156+
sum /= 10;
157+
}
158+
res.reverse();
159+
res
160+
}
161+
}
162+
```
163+
124164
### **...**
125165

126166
```

solution/0900-0999/0989.Add to Array-Form of Integer/README_EN.md

+40
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,46 @@ function addToArrayForm(num: number[], k: number): number[] {
106106
}
107107
```
108108

109+
```ts
110+
function addToArrayForm(num: number[], k: number): number[] {
111+
const n = num.length;
112+
const res = [];
113+
let sum = 0;
114+
for (let i = 0; i < n || sum !== 0 || k !== 0; i++) {
115+
sum += num[n - i - 1] ?? 0;
116+
sum += k % 10;
117+
res.push(sum % 10);
118+
k = Math.floor(k / 10);
119+
sum = Math.floor(sum / 10);
120+
}
121+
return res.reverse();
122+
}
123+
```
124+
125+
### **Rust**
126+
127+
```rust
128+
impl Solution {
129+
pub fn add_to_array_form(num: Vec<i32>, mut k: i32) -> Vec<i32> {
130+
let n = num.len();
131+
let mut res = vec![];
132+
let mut i = 0;
133+
let mut sum = 0;
134+
while i < n || sum != 0 || k != 0 {
135+
sum += num.get(n - i - 1).unwrap_or(&0);
136+
sum += k % 10;
137+
res.push(sum % 10);
138+
139+
i += 1;
140+
k /= 10;
141+
sum /= 10;
142+
}
143+
res.reverse();
144+
res
145+
}
146+
}
147+
```
148+
109149
### **...**
110150

111151
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
pub fn add_to_array_form(num: Vec<i32>, mut k: i32) -> Vec<i32> {
3+
let n = num.len();
4+
let mut res = vec![];
5+
let mut i = 0;
6+
let mut sum = 0;
7+
while i < n || sum != 0 || k != 0 {
8+
sum += num.get(n - i - 1).unwrap_or(&0);
9+
sum += k % 10;
10+
res.push(sum % 10);
11+
12+
i += 1;
13+
k /= 10;
14+
sum /= 10;
15+
}
16+
res.reverse();
17+
res
18+
}
19+
}

0 commit comments

Comments
 (0)