We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 1f27625 commit 7981f56Copy full SHA for 7981f56
67-add-binary.js
@@ -16,3 +16,28 @@ const addBinary = function(a, b) {
16
}
17
return s
18
};
19
+
20
+// another
21
22
+/**
23
+ * @param {string} a
24
+ * @param {string} b
25
+ * @return {string}
26
+ */
27
+const addBinary = function(a, b) {
28
+ let next = false
29
+ let res = []
30
+ let ai = a.length - 1
31
+ let bi = b.length - 1
32
+ while((ai >= 0 && bi >=0) || next) {
33
+ const tmp = (ai >= 0 ? +a[ai--] : 0) + (bi >= 0 ? +b[bi--] : 0) + (next ? 1 : 0)
34
+ if(tmp > 1) next = true
35
+ else next = false
36
+ res.unshift('' + (tmp % 2))
37
+ }
38
39
+ while(ai >= 0) res.unshift(a[ai--])
40
+ while(bi >= 0) res.unshift(b[bi--])
41
42
+ return res.join('')
43
+};
0 commit comments