-
Notifications
You must be signed in to change notification settings - Fork 0
/
061_AddBinary.java
116 lines (96 loc) · 3.21 KB
/
061_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Given two binary strings, return their sum (also a binary string).
*
* For example,
* a = "11"
* b = "1"
* Return "100".
*/
public class AddBinary67 {
public String addBinary(String a, String b) {
return addBinary(a, b, a.length()-1, b.length()-1, 0) ;
}
private String addBinary(String a, String b, int i, int j, int carry) {
if (i < 0 && j < 0) return (carry == 0) ? "" : "1";
int sum = carry;
if (i >= 0 && a.charAt(i) == '1') sum++;
if (j >= 0 && b.charAt(j) == '1') sum++;
return addBinary(a, b, i-1, j-1, (sum < 2) ? 0 : 1) + ((sum%2 == 0) ? "0" : "1");
}
public String addBinary2(String a, String b) {
StringBuilder sb = new StringBuilder();
addBinary(a, b, a.length()-1, b.length()-1, 0, sb) ;
return sb.toString();
}
private void addBinary(String a, String b, int i, int j, int carry, StringBuilder sb) {
if (i < 0 && j < 0) {
sb.append((carry == 0) ? "" : "1");
return;
}
int sum = carry;
if (i >= 0 && a.charAt(i) == '1') sum++;
if (j >= 0 && b.charAt(j) == '1') sum++;
addBinary(a, b, i-1, j-1, (sum < 2) ? 0 : 1, sb);
sb.append((sum%2 == 0) ? '0' : '1');
}
public String addBinary3(String a, String b) {
StringBuilder sb = new StringBuilder();
int carry = 0;
int i = a.length()-1;
int j = b.length()-1;
while (i >= 0 && j >= 0) {
int sum = carry;
if (a.charAt(i) == '1') sum++;
if (b.charAt(j) == '1') sum++;
carry = (sum < 2) ? 0 : 1;
sb.insert(0, (sum%2 == 0) ? '0' : '1');
i--;
j--;
}
while (i >= 0) {
int sum = carry;
if (a.charAt(i) == '1') sum++;
carry = (sum < 2) ? 0 : 1;
sb.insert(0, (sum%2 == 0) ? '0' : '1');
i--;
}
while (j >= 0) {
int sum = carry;
if (b.charAt(j) == '1') sum++;
carry = (sum < 2) ? 0 : 1;
sb.insert(0, (sum%2 == 0) ? '0' : '1');
j--;
}
if (carry == 1) sb.insert(0, '1');
return sb.toString();
}
public String addBinary4(String a, String b) {
char[] intToChar = new char[]{'0', '1'};
int len = Math.max(a.length(), b.length());
char[] res = new char[len + 1];
int carry = 0;
int s = len;
int i = a.length() - 1;
int j = b.length() - 1;
while (i >= 0 && j >= 0) {
int sum = charToInt(a.charAt(i--)) + charToInt(b.charAt(j--)) + carry;
carry = sum >> 1;
res[s--] = intToChar[sum & 1];
}
while (i >= 0) {
int sum = charToInt(a.charAt(i--)) + carry;
carry = sum >> 1;
res[s--] = intToChar[sum & 1];
}
while (j >= 0) {
int sum = charToInt(b.charAt(j--)) + carry;
carry = sum >> 1;
res[s--] = intToChar[sum & 1];
}
res[0] = intToChar[carry];
return res[0] == '0' ? (new String(res)).substring(1) : new String(res);
}
private int charToInt(char c) {
return c - '0';
}
}