Skip to content

Commit 4104388

Browse files
author
Tushar Borole
committed
67. Add Binary
1 parent 3441771 commit 4104388

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

.idea/workspace.xml

Lines changed: 16 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
|:-----|:--------------------------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------|:-----------------|:---------------------|:-----------|:----------------|:-----------------|:------------------------------------------------------------------|
1010
| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Solution](to_lower_case.js) | 72 ms | 32.1 MB | Easy | | | |
1111
| 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [iterative](best_time_to_buy_and_sell_stock_II.js) | 56 ms | 35.4 MB | Easy | O(N) | O(1) | |
12-
| 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | |
12+
| 1122 | [Relative Sort Array](https://leetcode.com/problems/relative-sort-array/) | [Ittrative](relative_sort_array.js) | 52 ms | 34.9 MB | Easy | | | |
13+
| 67 | [Add Binary](https://leetcode.com/problems/add-binary/) | [addBinary.js](addBinary.js) | 72 ms | 35.7 MB | Easy | | | [:link:](https://www.youtube.com/watch?v=6axItxXHouA) |
1314
| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Recursive](reverse_string.js) | 112 ms | 48.6 MB | Easy | 0(N) | 0(1) | |
1415
| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Solution](fibonacci_number.js) | 76 ms | 34.3 MB | Easy | | | |
1516
| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Recursive](climbing_stairs.js) | 52 ms | 33.9 MB | Easy | 0(N) | O(N) | [:link:](https://www.youtube.com/watch?v=NFJ3m9a1oJQ) |

addBinary.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {string} a
3+
* @param {string} b
4+
* @return {string}
5+
*/
6+
var addBinary = function(a, b) {
7+
let carry = 0;
8+
let out = "";
9+
if (a.length !== b.length) {
10+
if (a.length < b.length) {
11+
a = "0".repeat(b.length - a.length) + a;
12+
} else {
13+
b = "0".repeat(a.length - b.length) + b;
14+
}
15+
}
16+
let i = a.length - 1;
17+
let j = b.length - 1;
18+
19+
while (i >= 0 || j >= 0) {
20+
let aval = a[i];
21+
let bval = b[j];
22+
out = String(aval ^ bval ^ String(carry)) + out;
23+
if (aval === bval && aval !== String(carry)) {
24+
carry = Number(!carry);
25+
}
26+
i--;
27+
j--;
28+
}
29+
30+
return carry ? String(carry) + out : out;
31+
};
32+
addBinary("101111", "10"); //?

0 commit comments

Comments
 (0)