Skip to content

Commit 67905ed

Browse files
committed
feat: add solutions to lc problem: No.1073
No.1073.Adding Two Negabinary Numbers
1 parent df94e79 commit 67905ed

File tree

8 files changed

+135
-61
lines changed

8 files changed

+135
-61
lines changed

solution/1000-1099/1073.Adding Two Negabinary Numbers/README.md

+49-22
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56-
**方法一:进位转换**
56+
**方法一:模拟**
5757

5858
我们遍历两个数组,从最低位开始,记两个数组当前位的数字为 $a$ 和 $b$,进位为 $c$,三个数相加的结果为 $x$。
5959

60-
- 如果 $x \gt 1$,那么执行操作:将 $x$ 减去 $2$,并向高位进位 $-1$。
61-
- 如果 $x \lt 0$,那么执行操作:将 $x$ 加上 $2$,并向高位进位 $1$。
60+
- 先将进位 $c$ 置为 $0$。
61+
- 如果 $x \geq 2$,那么将 $x$ 减去 $2$,并向高位进位 $-1$。即逢 $2$ 进负 $1$。
62+
- 如果 $x = -1$,由于 $-(-2)^{i} = (-2)^{i} + (-2)^{i+1}$,所以我们可以将 $x$ 置为 $1$,并向高位进位 $1$。
6263

6364
然后,我们将 $x$ 加入到答案数组中,然后继续处理下一位。
6465

@@ -87,11 +88,11 @@ class Solution:
8788
b = 0 if j < 0 else arr2[j]
8889
x = a + b + c
8990
c = 0
90-
if x > 1:
91+
if x >= 2:
9192
x -= 2
9293
c -= 1
93-
if x < 0:
94-
x += 2
94+
elif x == -1:
95+
x = 1
9596
c += 1
9697
ans.append(x)
9798
i, j = i - 1, j - 1
@@ -114,12 +115,11 @@ class Solution {
114115
int b = j < 0 ? 0 : arr2[j];
115116
int x = a + b + c;
116117
c = 0;
117-
if (x > 1) {
118+
if (x >= 2) {
118119
x -= 2;
119120
c -= 1;
120-
}
121-
if (x < 0) {
122-
x += 2;
121+
} else if (x == -1) {
122+
x = 1;
123123
c += 1;
124124
}
125125
ans.add(x);
@@ -146,12 +146,11 @@ public:
146146
int b = j < 0 ? 0 : arr2[j];
147147
int x = a + b + c;
148148
c = 0;
149-
if (x > 1) {
149+
if (x >= 2) {
150150
x -= 2;
151151
c -= 1;
152-
}
153-
if (x < 0) {
154-
x += 2;
152+
} else if (x == -1) {
153+
x = 1;
155154
c += 1;
156155
}
157156
ans.push_back(x);
@@ -179,12 +178,11 @@ func addNegabinary(arr1 []int, arr2 []int) (ans []int) {
179178
x += arr2[j]
180179
}
181180
c = 0
182-
if x > 1 {
181+
if x >= 2 {
183182
x -= 2
184183
c -= 1
185-
}
186-
if x < 0 {
187-
x += 2
184+
} else if x == -1 {
185+
x = 1
188186
c += 1
189187
}
190188
ans = append(ans, x)
@@ -211,12 +209,11 @@ function addNegabinary(arr1: number[], arr2: number[]): number[] {
211209
const b = j < 0 ? 0 : arr2[j];
212210
let x = a + b + c;
213211
c = 0;
214-
if (x > 1) {
212+
if (x >= 2) {
215213
x -= 2;
216214
c -= 1;
217-
}
218-
if (x < 0) {
219-
x += 2;
215+
} else if (x === -1) {
216+
x = 1;
220217
c += 1;
221218
}
222219
ans.push(x);
@@ -228,6 +225,36 @@ function addNegabinary(arr1: number[], arr2: number[]): number[] {
228225
}
229226
```
230227

228+
### **C#**
229+
230+
```cs
231+
public class Solution {
232+
public int[] AddNegabinary(int[] arr1, int[] arr2) {
233+
int i = arr1.Length - 1, j = arr2.Length - 1;
234+
List<int> ans = new List<int>();
235+
for (int c = 0; i >= 0 || j >= 0 || c != 0; --i, --j) {
236+
int a = i < 0 ? 0 : arr1[i];
237+
int b = j < 0 ? 0 : arr2[j];
238+
int x = a + b + c;
239+
c = 0;
240+
if (x >= 2) {
241+
x -= 2;
242+
c -= 1;
243+
} else if (x == -1) {
244+
x = 1;
245+
c = 1;
246+
}
247+
ans.Add(x);
248+
}
249+
while (ans.Count > 1 && ans[ans.Count - 1] == 0) {
250+
ans.RemoveAt(ans.Count - 1);
251+
}
252+
ans.Reverse();
253+
return ans.ToArray();
254+
}
255+
}
256+
```
257+
231258
### **...**
232259

233260
```

solution/1000-1099/1073.Adding Two Negabinary Numbers/README_EN.md

+45-19
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ class Solution:
5959
b = 0 if j < 0 else arr2[j]
6060
x = a + b + c
6161
c = 0
62-
if x > 1:
62+
if x >= 2:
6363
x -= 2
6464
c -= 1
65-
if x < 0:
66-
x += 2
65+
elif x == -1:
66+
x = 1
6767
c += 1
6868
ans.append(x)
6969
i, j = i - 1, j - 1
@@ -84,12 +84,11 @@ class Solution {
8484
int b = j < 0 ? 0 : arr2[j];
8585
int x = a + b + c;
8686
c = 0;
87-
if (x > 1) {
87+
if (x >= 2) {
8888
x -= 2;
8989
c -= 1;
90-
}
91-
if (x < 0) {
92-
x += 2;
90+
} else if (x == -1) {
91+
x = 1;
9392
c += 1;
9493
}
9594
ans.add(x);
@@ -116,12 +115,11 @@ public:
116115
int b = j < 0 ? 0 : arr2[j];
117116
int x = a + b + c;
118117
c = 0;
119-
if (x > 1) {
118+
if (x >= 2) {
120119
x -= 2;
121120
c -= 1;
122-
}
123-
if (x < 0) {
124-
x += 2;
121+
} else if (x == -1) {
122+
x = 1;
125123
c += 1;
126124
}
127125
ans.push_back(x);
@@ -149,12 +147,11 @@ func addNegabinary(arr1 []int, arr2 []int) (ans []int) {
149147
x += arr2[j]
150148
}
151149
c = 0
152-
if x > 1 {
150+
if x >= 2 {
153151
x -= 2
154152
c -= 1
155-
}
156-
if x < 0 {
157-
x += 2
153+
} else if x == -1 {
154+
x = 1
158155
c += 1
159156
}
160157
ans = append(ans, x)
@@ -181,12 +178,11 @@ function addNegabinary(arr1: number[], arr2: number[]): number[] {
181178
const b = j < 0 ? 0 : arr2[j];
182179
let x = a + b + c;
183180
c = 0;
184-
if (x > 1) {
181+
if (x >= 2) {
185182
x -= 2;
186183
c -= 1;
187-
}
188-
if (x < 0) {
189-
x += 2;
184+
} else if (x === -1) {
185+
x = 1;
190186
c += 1;
191187
}
192188
ans.push(x);
@@ -198,6 +194,36 @@ function addNegabinary(arr1: number[], arr2: number[]): number[] {
198194
}
199195
```
200196

197+
### **C#**
198+
199+
```cs
200+
public class Solution {
201+
public int[] AddNegabinary(int[] arr1, int[] arr2) {
202+
int i = arr1.Length - 1, j = arr2.Length - 1;
203+
List<int> ans = new List<int>();
204+
for (int c = 0; i >= 0 || j >= 0 || c != 0; --i, --j) {
205+
int a = i < 0 ? 0 : arr1[i];
206+
int b = j < 0 ? 0 : arr2[j];
207+
int x = a + b + c;
208+
c = 0;
209+
if (x >= 2) {
210+
x -= 2;
211+
c -= 1;
212+
} else if (x == -1) {
213+
x = 1;
214+
c = 1;
215+
}
216+
ans.Add(x);
217+
}
218+
while (ans.Count > 1 && ans[ans.Count - 1] == 0) {
219+
ans.RemoveAt(ans.Count - 1);
220+
}
221+
ans.Reverse();
222+
return ans.ToArray();
223+
}
224+
}
225+
```
226+
201227
### **...**
202228

203229
```

solution/1000-1099/1073.Adding Two Negabinary Numbers/Solution.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ class Solution {
88
int b = j < 0 ? 0 : arr2[j];
99
int x = a + b + c;
1010
c = 0;
11-
if (x > 1) {
11+
if (x >= 2) {
1212
x -= 2;
1313
c -= 1;
14-
}
15-
if (x < 0) {
16-
x += 2;
14+
} else if (x == -1) {
15+
x = 1;
1716
c += 1;
1817
}
1918
ans.push_back(x);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public int[] AddNegabinary(int[] arr1, int[] arr2) {
3+
int i = arr1.Length - 1, j = arr2.Length - 1;
4+
List<int> ans = new List<int>();
5+
for (int c = 0; i >= 0 || j >= 0 || c != 0; --i, --j) {
6+
int a = i < 0 ? 0 : arr1[i];
7+
int b = j < 0 ? 0 : arr2[j];
8+
int x = a + b + c;
9+
c = 0;
10+
if (x >= 2) {
11+
x -= 2;
12+
c -= 1;
13+
} else if (x == -1) {
14+
x = 1;
15+
c = 1;
16+
}
17+
ans.Add(x);
18+
}
19+
while (ans.Count > 1 && ans[ans.Count - 1] == 0) {
20+
ans.RemoveAt(ans.Count - 1);
21+
}
22+
ans.Reverse();
23+
return ans.ToArray();
24+
}
25+
}

solution/1000-1099/1073.Adding Two Negabinary Numbers/Solution.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ func addNegabinary(arr1 []int, arr2 []int) (ans []int) {
99
x += arr2[j]
1010
}
1111
c = 0
12-
if x > 1 {
12+
if x >= 2 {
1313
x -= 2
1414
c -= 1
15-
}
16-
if x < 0 {
17-
x += 2
15+
} else if x == -1 {
16+
x = 1
1817
c += 1
1918
}
2019
ans = append(ans, x)

solution/1000-1099/1073.Adding Two Negabinary Numbers/Solution.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ public int[] addNegabinary(int[] arr1, int[] arr2) {
77
int b = j < 0 ? 0 : arr2[j];
88
int x = a + b + c;
99
c = 0;
10-
if (x > 1) {
10+
if (x >= 2) {
1111
x -= 2;
1212
c -= 1;
13-
}
14-
if (x < 0) {
15-
x += 2;
13+
} else if (x == -1) {
14+
x = 1;
1615
c += 1;
1716
}
1817
ans.add(x);

solution/1000-1099/1073.Adding Two Negabinary Numbers/Solution.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ def addNegabinary(self, arr1: List[int], arr2: List[int]) -> List[int]:
88
b = 0 if j < 0 else arr2[j]
99
x = a + b + c
1010
c = 0
11-
if x > 1:
11+
if x >= 2:
1212
x -= 2
1313
c -= 1
14-
if x < 0:
15-
x += 2
14+
elif x == -1:
15+
x = 1
1616
c += 1
1717
ans.append(x)
1818
i, j = i - 1, j - 1

solution/1000-1099/1073.Adding Two Negabinary Numbers/Solution.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ function addNegabinary(arr1: number[], arr2: number[]): number[] {
77
const b = j < 0 ? 0 : arr2[j];
88
let x = a + b + c;
99
c = 0;
10-
if (x > 1) {
10+
if (x >= 2) {
1111
x -= 2;
1212
c -= 1;
13-
}
14-
if (x < 0) {
15-
x += 2;
13+
} else if (x === -1) {
14+
x = 1;
1615
c += 1;
1716
}
1817
ans.push(x);
@@ -21,4 +20,4 @@ function addNegabinary(arr1: number[], arr2: number[]): number[] {
2120
ans.pop();
2221
}
2322
return ans.reverse();
24-
}
23+
}

0 commit comments

Comments
 (0)