forked from JadeZYX/Java_LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0067AddBinary.java
More file actions
48 lines (45 loc) · 1.44 KB
/
Copy pathP0067AddBinary.java
File metadata and controls
48 lines (45 loc) · 1.44 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
46
47
48
public class P0067AddBinary {
//这道题目与415题 add strings类似,一个是针对二进制,一个是针对十进制 /,%
public String addBinary0(String a, String b){
String res = "";
int index_a = a.length()-1;
int index_b = b.length()-1;
int carry = 0;//进位初始值
while(index_a>=0 || index_b>=0){
int aNum = index_a>=0? a.charAt(index_a)-'0':0;
//如果index没有out of boundary,则取对应位置上的字符,然后转成int类型,否则取0
int bNum = index_b>=0?b.charAt(index_b)-'0':0;
int tempSum = aNum + bNum + carry;
carry = tempSum/2;
int remain = tempSum % 2;
res = String.valueOf(remain)+res;
index_a--;
index_b--;
}
if(carry ==1) res = String.valueOf(carry)+res;
return res;
}
public String addBinary(String a, String b){//StringBuilder的效率会更高
StringBuilder sb = new StringBuilder();
int i = a.length()-1;
int j = b.length()-1;
int carry = 0;
while(i>=0 || j>=0){
int numA = i>=0?a.charAt(i)-'0':0;
int numB = j>=0?b.charAt(j)-'0':0;
int sum = numA + numB + carry;
carry = sum /2;
int remain = sum % 2;
sb.append(remain);
i--;
j--;
}
if(carry ==1) sb.append(carry);
return sb.reverse().toString();
}
}
/*
P0067AddBinary p67 = new P0067AddBinary();
System.out.println(p67.addBinary("11", "1"));
System.out.println(p67.addBinary("1010", "1011"));
*/