-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddBinary.java
More file actions
57 lines (44 loc) · 1.26 KB
/
Copy pathAddBinary.java
File metadata and controls
57 lines (44 loc) · 1.26 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
49
50
51
52
53
54
55
56
57
package com.leetcode.string;
public class AddBinary {
public String addBinary(String a, String b) {
if(a.length()<b.length()){
return compute(a, b);
}
return compute(b, a);
}
public String compute(String s1,String s2){
int n=s1.length()-1,m=s2.length()-1;
char[] res = new char[m + 2];
char bit = '0';
int p=res.length-1;
while (n>=0){
char c1 = s1.charAt(n--);
char c2 = s2.charAt(m--);
if(c1!=c2){
res[p--] = (bit == '0') ? '1' : '0';
}else {
res[p--] = (bit == '0') ? '0' : '1';
bit = (c1 == '1') ? '1' : '0';
}
}
while (m>=0){
char c = s2.charAt(m--);
if(c!=bit){
res[p--] = '1';
bit = '0';
}else {
res[p--] ='0';
}
}
int offset=1;
if(bit=='1') {
res[p] = '1';
offset=0;
}
return String.copyValueOf(res, offset, res.length-offset);
}
public static void main(String[] args) {
AddBinary binary = new AddBinary();
System.out.println(binary.addBinary("100","110010"));
}
}