forked from licong1112/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddBinary.java
56 lines (49 loc) · 1.51 KB
/
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
package com.congli.leetcode;
/**
* Practiced on 11/19/2012.
*
* Given two binary strings, return their sum (also a binary string).
* For example,
* a = "11"
* b = "1"
* Return "100"
*/
public class AddBinary {
public static void main(String[] args)
{
AddBinary sl = new AddBinary();
System.out.println(sl.addBinary("1", "111"));
}
public String addBinary(String a, String b) {
if(a == null || a == "") return b;
if(b == null || a == "") return a;
char[] resultChar = new char[Math.max(a.length(), b.length())+1];
resultChar[0] = '0';
int aInd = a.length()-1;
int bInd = b.length()-1;
char[] carry = {'0'};
for(int i = Math.max(aInd, bInd)+1; i >= 0; --i)
{
if(aInd < 0 && bInd < 0) resultChar[i] = carry[0];
else if(aInd < 0) resultChar[i] = helper(b.charAt(bInd--), '0', carry);
else if(bInd < 0) resultChar[i] = helper(a.charAt(aInd--), '0', carry);
else resultChar[i] = helper(a.charAt(aInd--), b.charAt(bInd--), carry);
}
String result = "";
for(int i = (resultChar[0] == '0' ? 1 : 0); i < resultChar.length; ++i)
{
result += resultChar[i];
}
return result;
}
private char helper(char a, char b, char[] carry)
{
if(a == b)
{
char result = carry[0] == '1' ? '1' : '0';
carry[0] = a == '1' ? '1' : '0';
return result;
}
else return carry[0] == '1' ? '0' : '1';
}
}