|
53 | 53 |
|
54 | 54 | <!-- 这里可写通用的实现逻辑 -->
|
55 | 55 |
|
| 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 | + |
56 | 68 | <!-- tabs:start -->
|
57 | 69 |
|
58 | 70 | ### **Python3**
|
59 | 71 |
|
60 | 72 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
61 | 73 |
|
62 | 74 | ```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] |
64 | 96 | ```
|
65 | 97 |
|
66 | 98 | ### **Java**
|
67 | 99 |
|
68 | 100 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
69 | 101 |
|
70 | 102 | ```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 | +``` |
71 | 162 |
|
| 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 | +} |
72 | 195 | ```
|
73 | 196 |
|
74 | 197 | ### **...**
|
|
0 commit comments