-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddBinary.java
More file actions
77 lines (69 loc) · 1.76 KB
/
Copy pathAddBinary.java
File metadata and controls
77 lines (69 loc) · 1.76 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
71
72
73
74
75
76
package Strings;
import org.junit.Test;
/**
* @description: 描述 Easy
* @author: dekai.kong
* @date: 2018-12-13 17:22
* @from https://leetcode.com/problems/add-binary/
* 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 AddBinary() {
}
/**
* Runtime: 2 ms, faster than 97.12% of Java online submissions for Add Binary.
* 遍历到底
* @param a
* @param b
* @return
*/
public String addBinary(String a, String b) {
char[] ax = a.toCharArray();
char[] bx = b.toCharArray();
int up = 0;
int la = ax.length-1;
int lb = bx.length-1;
StringBuilder sb = new StringBuilder();
while(la>=0||lb>=0){
int avl = 0;
int bvl = 0;
if(la>=0){
avl = ax[la]=='0' ?0:1;
}
if(lb>=0){
bvl = bx[lb]=='0' ?0:1;;
}
if ((avl & bvl) == 1 ){
sb.insert(0,up);
up = 1;
}else if(((avl ^ bvl) & up) == 1){
sb.insert(0,0);
up = 1;
}else{
sb.insert(0,(avl ^ bvl)+up);
up = 0;
}
la--;
lb--;
}
if (up == 1) {sb.insert(0,1);}
return sb.toString();
}
@Test
public void test() {
System.out.println(addBinary("1010","1011"));
System.out.println(addBinary("1111","1111"));
System.out.println(addBinary("11","1"));
}
}