-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAddBinary.java
More file actions
70 lines (66 loc) · 1.77 KB
/
AddBinary.java
File metadata and controls
70 lines (66 loc) · 1.77 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
58
59
60
61
62
63
64
65
66
67
68
69
70
package leetcode;
import java.util.Arrays;
/**
* @author eko
* @date 2019/6/22 15:58
*
* 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"
*/
public class AddBinary {
public static void main(String[] args) {
String a = "1010";
String b = "1011";
System.out.println(addBinary(a, b));
}
public static String addBinary(String a, String b) {
char[] as = a.toCharArray();
char[] bs = b.toCharArray();
int length = Math.max(as.length, bs.length);
char[] result = new char[length + 1];
char flag = '0';
int i = as.length;
int j = bs.length;
while (i > 0 || j >0) {
int total = flag - '0';
if (i > 0) total = total + as[i-1] - '0';
if (j > 0) total = total + bs[j-1] - '0';
switch (total) {
case 0:
result[length] = '0';
flag = '0';
break;
case 1:
result[length] = '1';
flag = '0';
break;
case 2:
result[length] = '0';
flag = '1';
break;
case 3:
result[length] = '1';
flag = '1';
break;
}
length--;
i--;
j--;
}
if (flag == '1') {
result[0] = '1';
return new String(result);
}
return new String(result).substring(1);
}
}