-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddBinary.java
41 lines (39 loc) · 864 Bytes
/
AddBinary.java
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
// Link: https://leetcode.com/problems/add-binary/
/*
* 2ms, 75.29%
*
* The idea is similar to problem 2-Add-Two-Numbers (../2-Add-Two-Numbers/AddTwoNumbers.java)
*/
class Solution {
public String addBinary(String a, String b) {
StringBuilder sb = new StringBuilder();
int aLen = a.length();
int bLen = b.length();
int carry = 0;
for (int i = 0; i < aLen || i < bLen; ++i) {
int sum = carry;
int aIdx = aLen - 1 - i;
int bIdx = bLen - 1 - i;
if (aIdx >= 0) {
sum += a.charAt(aIdx) - '0';
}
if (bIdx >= 0) {
sum += b.charAt(bIdx) - '0';
}
sb.append(sum % 2);
carry = sum / 2;
}
if (carry != 0) {
sb.append(carry);
}
return sb.reverse().toString();
}
}
/*
* Time complexity: O(max(alen, blen))
*
* Space complexity: O(1)
*
* NOTES:
*
*/