-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathE_AddBinary.js
47 lines (40 loc) · 934 Bytes
/
E_AddBinary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
*
* */
//ac 150ms
function addBinary(a, b){
var m = a.length, n = b.length;
var indexa, indexb;
var temp = 0, add = 0; //store temp cal value
var result = "";
//offset i
for(var i = 1; i <= Math.max(m, n); i++){
indexa = m - i;
indexb = n - i;
if (indexa >= 0 && indexb >= 0) {
temp = add + parseInt(a.charAt(indexa)) + parseInt(b.charAt(indexb));
} else if(indexb < 0) {
temp = add + parseInt(a.charAt(indexa));
} else if(indexa < 0) {
temp = add + parseInt(b.charAt(indexb));
}
if (temp > 1) {
add = 1;
temp -= 2;
} else {
add = 0;
}
result = temp + result; //add temp to the first digit
}
//handle the first digit for add
if(add == 1){
result = add + result; //add 1 to the first digit
}
return result;
}
console.log(addBinary("10", "1010"));