Skip to content

Commit 23d3dc3

Browse files
committed
feat: add solutions to lc problem: No.1073
No.1073.Adding Two Negabinary Numbers
1 parent cd70a60 commit 23d3dc3

File tree

7 files changed

+338
-14
lines changed

7 files changed

+338
-14
lines changed

Diff for: solution/1000-1099/1017.Convert to Base -2/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656

5757
时间复杂度 $O(\log n)$,其中 $n$ 为给定的整数。忽略答案的空间消耗,空间复杂度 $O(1)$。
5858

59+
相似题目:
60+
61+
- [1073. 负二进制数相加](/solution/1000-1099/1073.Adding%20Two%20Negabinary%20Numbers/README.md)
62+
5963
<!-- tabs:start -->
6064

6165
### **Python3**

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

+124-1
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,145 @@
5353

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

56+
**方法一:进位转换**
57+
58+
如果两个数对应的位与进位 $c$ 相加的结果大于 $1$,那么先执行操作:将结果减去 $2$,并向高位进位 $-1$。如果相加的结果为 $-1$,那么执行操作:将结果加上 $2$,并向高位进位 $1$。此时我们将结果加入到答案数组中,然后继续处理下一位。
59+
60+
最后,我们需要去除答案数组中末尾的 $0$,并将数组反转,即可得到最终的答案。
61+
62+
时间复杂度 $O(\max(n, m))$,其中 $n$ 和 $m$ 分别是两个数组的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。
63+
64+
相似题目:
65+
66+
- [1017. 负二进制转换](/solution/1000-1099/1017.Convert%20to%20Base%20-2/README.md)
67+
5668
<!-- tabs:start -->
5769

5870
### **Python3**
5971

6072
<!-- 这里可写当前语言的特殊实现逻辑 -->
6173

6274
```python
63-
75+
class Solution:
76+
def addNegabinary(self, arr1: List[int], arr2: List[int]) -> List[int]:
77+
i, j = len(arr1) - 1, len(arr2) - 1
78+
c = 0
79+
ans = []
80+
while i >= 0 or j >= 0 or c:
81+
a = 0 if i < 0 else arr1[i]
82+
b = 0 if j < 0 else arr2[j]
83+
x = a + b + c
84+
c = 0
85+
if x > 1:
86+
x -= 2
87+
c -= 1
88+
if x < 0:
89+
x += 2
90+
c += 1
91+
ans.append(x)
92+
i, j = i - 1, j - 1
93+
while len(ans) > 1 and ans[-1] == 0:
94+
ans.pop()
95+
return ans[::-1]
6496
```
6597

6698
### **Java**
6799

68100
<!-- 这里可写当前语言的特殊实现逻辑 -->
69101

70102
```java
103+
class Solution {
104+
public int[] addNegabinary(int[] arr1, int[] arr2) {
105+
int i = arr1.length - 1, j = arr2.length - 1;
106+
List<Integer> ans = new ArrayList<>();
107+
for (int c = 0; i >= 0 || j >= 0 || c != 0; --i, --j) {
108+
int a = i < 0 ? 0 : arr1[i];
109+
int b = j < 0 ? 0 : arr2[j];
110+
int x = a + b + c;
111+
c = 0;
112+
if (x > 1) {
113+
x -= 2;
114+
c -= 1;
115+
}
116+
if (x < 0) {
117+
x += 2;
118+
c += 1;
119+
}
120+
ans.add(x);
121+
}
122+
while (ans.size() > 1 && ans.get(ans.size() - 1) == 0) {
123+
ans.remove(ans.size() -1);
124+
}
125+
Collections.reverse(ans);
126+
return ans.stream().mapToInt(x -> x).toArray();
127+
}
128+
}
129+
```
130+
131+
### **C++**
132+
133+
```cpp
134+
class Solution {
135+
public:
136+
vector<int> addNegabinary(vector<int>& arr1, vector<int>& arr2) {
137+
int i = arr1.size() - 1, j = arr2.size() - 1;
138+
vector<int> ans;
139+
for (int c = 0; i >= 0 || j >= 0 || c; --i, --j) {
140+
int a = i < 0 ? 0 : arr1[i];
141+
int b = j < 0 ? 0 : arr2[j];
142+
int x = a + b + c;
143+
c = 0;
144+
if (x > 1) {
145+
x -= 2;
146+
c -= 1;
147+
}
148+
if (x < 0) {
149+
x += 2;
150+
c += 1;
151+
}
152+
ans.push_back(x);
153+
}
154+
while (ans.size() > 1 && ans.back() == 0) {
155+
ans.pop_back();
156+
}
157+
reverse(ans.begin(), ans.end());
158+
return ans;
159+
}
160+
};
161+
```
71162
163+
### **Go**
164+
165+
```go
166+
func addNegabinary(arr1 []int, arr2 []int) (ans []int) {
167+
i, j := len(arr1)-1, len(arr2)-1
168+
for c := 0; i >= 0 || j >= 0 || c != 0; i, j = i-1, j-1 {
169+
x := c
170+
if i >= 0 {
171+
x += arr1[i]
172+
}
173+
if j >= 0 {
174+
x += arr2[j]
175+
}
176+
c = 0
177+
if x > 1 {
178+
x -= 2
179+
c -= 1
180+
}
181+
if x < 0 {
182+
x += 2
183+
c += 1
184+
}
185+
ans = append(ans, x)
186+
}
187+
for len(ans) > 1 && ans[len(ans)-1] == 0 {
188+
ans = ans[:len(ans)-1]
189+
}
190+
for i, j = 0, len(ans)-1; i < j; i, j = i+1, j-1 {
191+
ans[i], ans[j] = ans[j], ans[i]
192+
}
193+
return ans
194+
}
72195
```
73196

74197
### **...**

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

+112-1
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,124 @@
4949
### **Python3**
5050

5151
```python
52-
52+
class Solution:
53+
def addNegabinary(self, arr1: List[int], arr2: List[int]) -> List[int]:
54+
i, j = len(arr1) - 1, len(arr2) - 1
55+
c = 0
56+
ans = []
57+
while i >= 0 or j >= 0 or c:
58+
a = 0 if i < 0 else arr1[i]
59+
b = 0 if j < 0 else arr2[j]
60+
x = a + b + c
61+
c = 0
62+
if x > 1:
63+
x -= 2
64+
c -= 1
65+
if x < 0:
66+
x += 2
67+
c += 1
68+
ans.append(x)
69+
i, j = i - 1, j - 1
70+
while len(ans) > 1 and ans[-1] == 0:
71+
ans.pop()
72+
return ans[::-1]
5373
```
5474

5575
### **Java**
5676

5777
```java
78+
class Solution {
79+
public int[] addNegabinary(int[] arr1, int[] arr2) {
80+
int i = arr1.length - 1, j = arr2.length - 1;
81+
List<Integer> ans = new ArrayList<>();
82+
for (int c = 0; i >= 0 || j >= 0 || c != 0; --i, --j) {
83+
int a = i < 0 ? 0 : arr1[i];
84+
int b = j < 0 ? 0 : arr2[j];
85+
int x = a + b + c;
86+
c = 0;
87+
if (x > 1) {
88+
x -= 2;
89+
c -= 1;
90+
}
91+
if (x < 0) {
92+
x += 2;
93+
c += 1;
94+
}
95+
ans.add(x);
96+
}
97+
while (ans.size() > 1 && ans.get(ans.size() - 1) == 0) {
98+
ans.remove(ans.size() -1);
99+
}
100+
Collections.reverse(ans);
101+
return ans.stream().mapToInt(x -> x).toArray();
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
vector<int> addNegabinary(vector<int>& arr1, vector<int>& arr2) {
112+
int i = arr1.size() - 1, j = arr2.size() - 1;
113+
vector<int> ans;
114+
for (int c = 0; i >= 0 || j >= 0 || c; --i, --j) {
115+
int a = i < 0 ? 0 : arr1[i];
116+
int b = j < 0 ? 0 : arr2[j];
117+
int x = a + b + c;
118+
c = 0;
119+
if (x > 1) {
120+
x -= 2;
121+
c -= 1;
122+
}
123+
if (x < 0) {
124+
x += 2;
125+
c += 1;
126+
}
127+
ans.push_back(x);
128+
}
129+
while (ans.size() > 1 && ans.back() == 0) {
130+
ans.pop_back();
131+
}
132+
reverse(ans.begin(), ans.end());
133+
return ans;
134+
}
135+
};
136+
```
58137
138+
### **Go**
139+
140+
```go
141+
func addNegabinary(arr1 []int, arr2 []int) (ans []int) {
142+
i, j := len(arr1)-1, len(arr2)-1
143+
for c := 0; i >= 0 || j >= 0 || c != 0; i, j = i-1, j-1 {
144+
x := c
145+
if i >= 0 {
146+
x += arr1[i]
147+
}
148+
if j >= 0 {
149+
x += arr2[j]
150+
}
151+
c = 0
152+
if x > 1 {
153+
x -= 2
154+
c -= 1
155+
}
156+
if x < 0 {
157+
x += 2
158+
c += 1
159+
}
160+
ans = append(ans, x)
161+
}
162+
for len(ans) > 1 && ans[len(ans)-1] == 0 {
163+
ans = ans[:len(ans)-1]
164+
}
165+
for i, j = 0, len(ans)-1; i < j; i, j = i+1, j-1 {
166+
ans[i], ans[j] = ans[j], ans[i]
167+
}
168+
return ans
169+
}
59170
```
60171

61172
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
vector<int> addNegabinary(vector<int>& arr1, vector<int>& arr2) {
4+
int i = arr1.size() - 1, j = arr2.size() - 1;
5+
vector<int> ans;
6+
for (int c = 0; i >= 0 || j >= 0 || c; --i, --j) {
7+
int a = i < 0 ? 0 : arr1[i];
8+
int b = j < 0 ? 0 : arr2[j];
9+
int x = a + b + c;
10+
c = 0;
11+
if (x > 1) {
12+
x -= 2;
13+
c -= 1;
14+
}
15+
if (x < 0) {
16+
x += 2;
17+
c += 1;
18+
}
19+
ans.push_back(x);
20+
}
21+
while (ans.size() > 1 && ans.back() == 0) {
22+
ans.pop_back();
23+
}
24+
reverse(ans.begin(), ans.end());
25+
return ans;
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func addNegabinary(arr1 []int, arr2 []int) (ans []int) {
2+
i, j := len(arr1)-1, len(arr2)-1
3+
for c := 0; i >= 0 || j >= 0 || c != 0; i, j = i-1, j-1 {
4+
x := c
5+
if i >= 0 {
6+
x += arr1[i]
7+
}
8+
if j >= 0 {
9+
x += arr2[j]
10+
}
11+
c = 0
12+
if x > 1 {
13+
x -= 2
14+
c -= 1
15+
}
16+
if x < 0 {
17+
x += 2
18+
c += 1
19+
}
20+
ans = append(ans, x)
21+
}
22+
for len(ans) > 1 && ans[len(ans)-1] == 0 {
23+
ans = ans[:len(ans)-1]
24+
}
25+
for i, j = 0, len(ans)-1; i < j; i, j = i+1, j-1 {
26+
ans[i], ans[j] = ans[j], ans[i]
27+
}
28+
return ans
29+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
class Solution {
22
public int[] addNegabinary(int[] arr1, int[] arr2) {
3-
List<Integer> list = new ArrayList<>();
4-
int carry = 0;
5-
for (int i = arr1.length - 1, j = arr2.length - 1; i >= 0 || j >= 0 || carry != 0;
6-
--i, --j) {
7-
carry += (i >= 0 ? arr1[i] : 0) + (j >= 0 ? arr2[j] : 0);
8-
list.add(carry & 1);
9-
carry = -(carry >> 1);
3+
int i = arr1.length - 1, j = arr2.length - 1;
4+
List<Integer> ans = new ArrayList<>();
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 > 1) {
11+
x -= 2;
12+
c -= 1;
13+
}
14+
if (x < 0) {
15+
x += 2;
16+
c += 1;
17+
}
18+
ans.add(x);
1019
}
11-
while (list.size() > 1 && list.get(list.size() - 1) == 0) {
12-
list.remove(list.size() - 1);
20+
while (ans.size() > 1 && ans.get(ans.size() - 1) == 0) {
21+
ans.remove(ans.size() -1);
1322
}
14-
Collections.reverse(list);
15-
return list.stream().mapToInt(x -> x).toArray();
23+
Collections.reverse(ans);
24+
return ans.stream().mapToInt(x -> x).toArray();
1625
}
17-
}
26+
}

0 commit comments

Comments
 (0)