-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path67.add-binary.java
More file actions
35 lines (33 loc) · 1006 Bytes
/
Copy path67.add-binary.java
File metadata and controls
35 lines (33 loc) · 1006 Bytes
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
/*
* @lc app=leetcode id=67 lang=java
*
* [67] Add Binary
*/
// @lc code=start
class Solution {
public String addBinary(String a, String b) {
int len_a = a.length() - 1;
int len_b = b.length() - 1;
int len = (len_a > len_b) ? len_a : len_b;
int carry = 0;
StringBuffer res = new StringBuffer();
while (len_a >= 0 || len_b >= 0) {
int x = (len_a >= 0) ? a.charAt(len_a--) - '0' : 0;
int y = (len_b >= 0) ? b.charAt(len_b--) - '0' : 0;
int sum = x + y + carry;
res.append(Integer.toString(sum % 2));
// res.insert(len + 1, Integer.toString(sum % 2));
/*
Previous solution causes REG error,
can't access position "len + 1".
*/
carry = sum / 2;
}
if (carry == 1) {
// res.insert(0, "1");
res.append("1");
}
return res.reverse().toString();
}
}
// @lc code=end