Skip to content

Commit f7ff7d4

Browse files
committed
Add solution #1073
1 parent 7d780c7 commit f7ff7d4

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,113 LeetCode solutions in JavaScript
1+
# 1,114 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -853,6 +853,7 @@
853853
1051|[Height Checker](./solutions/1051-height-checker.js)|Easy|
854854
1071|[Greatest Common Divisor of Strings](./solutions/1071-greatest-common-divisor-of-strings.js)|Easy|
855855
1072|[Flip Columns For Maximum Number of Equal Rows](./solutions/1072-flip-columns-for-maximum-number-of-equal-rows.js)|Medium|
856+
1073|[Adding Two Negabinary Numbers](./solutions/1073-adding-two-negabinary-numbers.js)|Medium|
856857
1079|[Letter Tile Possibilities](./solutions/1079-letter-tile-possibilities.js)|Medium|
857858
1081|[Smallest Subsequence of Distinct Characters](./solutions/1081-smallest-subsequence-of-distinct-characters.js)|Medium|
858859
1092|[Shortest Common Supersequence](./solutions/1092-shortest-common-supersequence.js)|Hard|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 1073. Adding Two Negabinary Numbers
3+
* https://leetcode.com/problems/adding-two-negabinary-numbers/
4+
* Difficulty: Medium
5+
*
6+
* Given two numbers arr1 and arr2 in base -2, return the result of adding them together.
7+
*
8+
* Each number is given in array format: as an array of 0s and 1s, from most significant
9+
* bit to least significant bit. For example, arr = [1,1,0,1] represents the number
10+
* (-2)^3 + (-2)^2 + (-2)^0 = -3. A number arr in array, format is also guaranteed to
11+
* have no leading zeros: either arr == [0] or arr[0] == 1.
12+
*
13+
* Return the result of adding arr1 and arr2 in the same format: as an array of 0s and 1s
14+
* with no leading zeros.
15+
*/
16+
17+
/**
18+
* @param {number[]} arr1
19+
* @param {number[]} arr2
20+
* @return {number[]}
21+
*/
22+
var addNegabinary = function(arr1, arr2) {
23+
const result = [];
24+
let i = arr1.length - 1;
25+
let j = arr2.length - 1;
26+
let carry = 0;
27+
28+
while (i >= 0 || j >= 0 || carry !== 0) {
29+
const x = i >= 0 ? arr1[i--] : 0;
30+
const y = j >= 0 ? arr2[j--] : 0;
31+
const sum = x + y + carry;
32+
33+
result.unshift(sum & 1);
34+
carry = sum >= 2 ? -1 : sum < 0 ? 1 : 0;
35+
}
36+
37+
while (result.length > 1 && result[0] === 0) {
38+
result.shift();
39+
}
40+
41+
return result;
42+
};

0 commit comments

Comments
 (0)