forked from soulmachine/algorithm-essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-binary.java
More file actions
19 lines (18 loc) · 627 Bytes
/
add-binary.java
File metadata and controls
19 lines (18 loc) · 627 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Add Binary
// 时间复杂度O(n),空间复杂度O(1)
class Solution {
public String addBinary(String a, String b) {
StringBuilder result = new StringBuilder();
int i = a.length() - 1;
int j = b.length() - 1;
int carry = 0;
while(i >= 0 || j >= 0 || carry > 0) {
int valueA = i < 0 ? 0 : a.charAt(i--) - '0';
int valueB = j < 0 ? 0 : b.charAt(j--) - '0';
int sum = valueA + valueB + carry;
result.insert(0, Character.forDigit(sum % 2, 10));
carry = sum / 2;
}
return result.toString();
}
}