forked from LjyYano/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL067_Add_Binary.java
56 lines (42 loc) · 1.03 KB
/
L067_Add_Binary.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
package LeetCode;
public class L067_Add_Binary {
public static String addBinary(String a, String b) {
if (a == null || b == null) {
return null;
}
char[] ca = a.toCharArray();
char[] cb = b.toCharArray();
int n = Math.max(ca.length, cb.length);
// 首先定义一个数组,个数为n+1,因为可能有一个进位
int[] s = new int[n + 1];
for (int i = 0; i < n; i++) {
// 按位相加,没有则返回0
// 进位到下一位
s[i] += toInt(ca, ca.length - 1 - i) + toInt(cb, cb.length - 1 - i);
s[i + 1] = s[i] / 2;
s[i] %= 2;
}
String result = "";
// s[n]存的是第一位,放入result要在前面(结果第0位是最高位)
for (int i = n - 1; i >= 0; i--) {
result = result + s[i];
}
// 如果s最高位是1,则在结果加入1,表示最高位
if (s[n] == 1) {
result = "1" + result;
}
return result;
}
public static int toInt(char c) {
if (c >= '0') {
return c - '0';
}
return 0;
}
public static int toInt(char[] chars, int index) {
if (index >= 0 && index < chars.length) {
return toInt(chars[index]);
}
return 0;
}
}