53
53
54
54
<!-- 这里可写通用的实现逻辑 -->
55
55
56
- ** 方法一:进位转换 **
56
+ ** 方法一:模拟 **
57
57
58
58
我们遍历两个数组,从最低位开始,记两个数组当前位的数字为 $a$ 和 $b$,进位为 $c$,三个数相加的结果为 $x$。
59
59
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$。
62
63
63
64
然后,我们将 $x$ 加入到答案数组中,然后继续处理下一位。
64
65
@@ -87,11 +88,11 @@ class Solution:
87
88
b = 0 if j < 0 else arr2[j]
88
89
x = a + b + c
89
90
c = 0
90
- if x > 1 :
91
+ if x >= 2 :
91
92
x -= 2
92
93
c -= 1
93
- if x < 0 :
94
- x += 2
94
+ elif x == - 1 :
95
+ x = 1
95
96
c += 1
96
97
ans.append(x)
97
98
i, j = i - 1 , j - 1
@@ -114,12 +115,11 @@ class Solution {
114
115
int b = j < 0 ? 0 : arr2[j];
115
116
int x = a + b + c;
116
117
c = 0 ;
117
- if (x > 1 ) {
118
+ if (x >= 2 ) {
118
119
x -= 2 ;
119
120
c -= 1 ;
120
- }
121
- if (x < 0 ) {
122
- x += 2 ;
121
+ } else if (x == - 1 ) {
122
+ x = 1 ;
123
123
c += 1 ;
124
124
}
125
125
ans. add(x);
@@ -146,12 +146,11 @@ public:
146
146
int b = j < 0 ? 0 : arr2[ j] ;
147
147
int x = a + b + c;
148
148
c = 0;
149
- if (x > 1 ) {
149
+ if (x >= 2 ) {
150
150
x -= 2;
151
151
c -= 1;
152
- }
153
- if (x < 0) {
154
- x += 2;
152
+ } else if (x == -1) {
153
+ x = 1;
155
154
c += 1;
156
155
}
157
156
ans.push_back(x);
@@ -179,12 +178,11 @@ func addNegabinary(arr1 []int, arr2 []int) (ans []int) {
179
178
x += arr2[j]
180
179
}
181
180
c = 0
182
- if x > 1 {
181
+ if x >= 2 {
183
182
x -= 2
184
183
c -= 1
185
- }
186
- if x < 0 {
187
- x += 2
184
+ } else if x == -1 {
185
+ x = 1
188
186
c += 1
189
187
}
190
188
ans = append(ans, x)
@@ -211,12 +209,11 @@ function addNegabinary(arr1: number[], arr2: number[]): number[] {
211
209
const b = j < 0 ? 0 : arr2 [j ];
212
210
let x = a + b + c ;
213
211
c = 0 ;
214
- if (x > 1 ) {
212
+ if (x >= 2 ) {
215
213
x -= 2 ;
216
214
c -= 1 ;
217
- }
218
- if (x < 0 ) {
219
- x += 2 ;
215
+ } else if (x === - 1 ) {
216
+ x = 1 ;
220
217
c += 1 ;
221
218
}
222
219
ans .push (x );
@@ -228,6 +225,36 @@ function addNegabinary(arr1: number[], arr2: number[]): number[] {
228
225
}
229
226
```
230
227
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
+
231
258
### ** ...**
232
259
233
260
```
0 commit comments