Skip to content

Commit abb6a9c

Browse files
committed
finish 0067
1 parent 4ebab0d commit abb6a9c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ since 2021.10.10 / Benben
3030
- 0035.Search Insert Position
3131
- 0053.Maximum SubArray
3232
- 0058.Length of Last Word
33+
- 0067.Add Binary
3334
- 0104.Maximum Depth of Binary Tree
3435
- 0100.Same Tree
3536
- 0110.Balanced Binary Tree

leetcode-easy/0067-add-binary.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @param {string} a
3+
* @param {string} b
4+
* @return {string}
5+
*/
6+
var addBinary = function(a, b) {
7+
if (a.length < b.length) return addBinary(b, a)
8+
const arrA = a.split('').reverse()
9+
const arrB = b.split('').reverse()
10+
let curry = 0
11+
let sum = 0
12+
13+
arrA.forEach((e, i) => {
14+
let numB = 0
15+
if (i < arrB.length) numB = +(arrB[i])
16+
sum = curry + (+e) + numB
17+
switch (sum) {
18+
case 0:
19+
arrA[i] = '0'
20+
curry = 0
21+
break
22+
case 1:
23+
arrA[i] = '1'
24+
curry = 0
25+
break
26+
case 2:
27+
arrA[i] = '0'
28+
curry = 1
29+
break
30+
case 3:
31+
arrA[i] = '1'
32+
curry = 1
33+
break
34+
}
35+
})
36+
37+
if (curry) arrA.push('1')
38+
return arrA.reverse().join('')
39+
};
40+
41+
// 2022/06/13 done.
42+
// Runtime: 105 ms, faster than 28.95% of JavaScript online submissions for Add Binary.
43+
// Memory Usage: 42.6 MB, less than 80.31% of JavaScript online submissions for Add Binary.

0 commit comments

Comments
 (0)