-
Notifications
You must be signed in to change notification settings - Fork 0
/
067_Add_Binary.java
47 lines (45 loc) · 1.04 KB
/
067_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
// Given two binary strings, return their sum (also a binary string).
//
// The input strings are both non-empty and contains only characters 1 or 0.
//
// Example 1:
//
//
// Input: a = "11", b = "1"
// Output: "100"
//
// Example 2:
//
//
// Input: a = "1010", b = "1011"
// Output: "10101"
//
class Solution {
public String addBinary(String a, String b) {
int i=a.length()-1;
int j=b.length()-1;
int carry=0;
StringBuffer res=new StringBuffer();
while(i>=0||j>=0){
int num1=(i>=0)?charToInt(a.charAt(i)):0;
int num2=(j>=0)?charToInt(b.charAt(j)):0;
int sum=num1+num2+carry;
carry=(sum>1)?1:0;
if(carry>0){
res.append(sum-2);
}else{
res.append(sum);
}
i--;
j--;
}
if(carry==1){
res.append(1);
}
return res.reverse().toString();
}
public int charToInt(char a){
if(a=='1') return 1;
return 0;
}
}