-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddBinary.java
64 lines (60 loc) · 1.75 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package easy;
/**
* Given two binary strings, return their sum (also a binary string).
* For example,
* a = "11"
* b = "1"
* Return "100".
*
* 解决思路:
* 二进制加法
*
* Created by second on 2017/9/2.
*/
public class AddBinary {
public String addBinary(String a, String b) {
// fix zero
int length = a.length() > b.length() ? a.length() : b.length();
char[] ac = new char[a.length()];
char[] bc = new char[b.length()];
a.getChars(0, a.length(), ac, 0);
b.getChars(0, b.length(), bc, 0);
char[] charsA = fixZero(ac, length);
char[] charsB = fixZero(bc, length);
// plus
StringBuilder result = new StringBuilder();
int into = 0;
for (int i = length - 1; i >= 0; i--){
int ai = Integer.parseInt(String.valueOf(charsA[i]));
int bi = Integer.parseInt(String.valueOf(charsB[i]));
int sum = ai + bi + into;
if (sum == 3){
result.append(1);
into = 1;
}
if (sum == 2){
result.append(0);
into = 1;
}
if (sum == 1){
result.append(1);
into = 0;
}
if (sum == 0){
result.append(0);
into = 0;
}
}
if (into == 1) result.append(1);
return result.reverse().toString();
}
private char[] fixZero(char[] c, int length){
if (c.length >= length) return c;
char[] result = new char[length];
for (int i = 0; i < length; i++){
if (i < length - c.length) result[i] = '0';
else result[i] = c[i - (length - c.length)];
}
return result;
}
}