-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAddBinary.java
More file actions
45 lines (41 loc) · 1.24 KB
/
AddBinary.java
File metadata and controls
45 lines (41 loc) · 1.24 KB
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
package string;
// Source : https://leetcode.com/problems/add-binary/
// Id : 67
// Author : Fanlu Hai | https://github.com/Fanlu91/FanluLeetcode
// Date : 2020-01-12
// Topic : String
// Level : Easy
// Other :
// Tips :
// Result : 97.15% 5.12%
public class AddBinary {
// 97.15% 2 ms 5.12%
public String addBinary(String a, String b) {
StringBuilder stringBuilder = new StringBuilder();
int carry = 0;
if (a.length() < b.length()) {
// String tmp = a;
// a = b;
// b = tmp;
return addBinary(b, a);
}
for (int i = 0; i < a.length(); i++) {
int valueA = a.charAt(a.length() - 1 - i) - '0';
int valueB = i < b.length() ? b.charAt(b.length() - 1 - i) - '0' : 0;
int sum = valueA + valueB + carry;
if (sum == 2) {
stringBuilder.append(0);
carry = 1;
} else if (sum == 3) {
stringBuilder.append(1);
carry = 1;
} else {
stringBuilder.append(sum);
carry = 0;
}
}
if (carry == 1)
stringBuilder.append(1);
return stringBuilder.reverse().toString();
}
}