-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddBinary.java
More file actions
69 lines (66 loc) · 2.12 KB
/
Copy pathAddBinary.java
File metadata and controls
69 lines (66 loc) · 2.12 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
package com.al.AddBinary;
/**
* lc #67 add binary
*/
public class AddBinary {
/**
* TODO use bit operation
*/
class Solution {
public String addBinary(String a, String b) {
StringBuilder sb = new StringBuilder();
int ap = a.length() - 1, bp = b.length() - 1;
char plus = '0';
char remain = '0';
while (ap >= 0 && bp >= 0) {
int sum = plus + a.charAt(ap--) + b.charAt(bp--) - 3 * '0';
if (sum == 0) {
plus = '0';
remain = '0';
} else if (sum == 1) {
plus = '0';
remain = '1';
} else if (sum == 2) {
plus = '1';
remain = '0';
} else if (sum == 3) {
plus = '1';
remain = '1';
}
sb.insert(0,remain);
}
if (ap != -1) {
plus = getPlus(a, sb, ap, plus, remain);
} else if (bp != -1){
plus = getPlus(b, sb, bp, plus, remain);
}
if (plus == '1') {
return plus + sb.toString();
}
return sb.toString();
}
private char getPlus(String a, StringBuilder sb, int ap, char plus, char remain) {
while (ap >= 0) {
int sum = plus + a.charAt(ap--) - 2 * '0';
if (sum == 0) {
plus = '0';
remain = '0';
} else if (sum == 1) {
plus = '0';
remain = '1';
} else if (sum == 2) {
plus = '1';
remain = '0';
}
sb.insert(0,remain);
}
return plus;
}
}
public static void main(String[] args) {
Solution sl = new AddBinary().new Solution();
System.out.println(sl.addBinary("11", "1"));
System.out.println(sl.addBinary("1010", "1011"));
System.out.println(sl.addBinary("1", "1"));
}
}