Skip to content

Commit e888cce

Browse files
committed
0067. Add Binary
1 parent 940deac commit e888cce

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed

markdown/0067. Add Binary.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
### [67\. Add Binary](https://leetcode.com/problems/add-binary/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given two binary strings, return their sum (also a binary string).
7+
8+
The input strings are both **non-empty** and contains only characters `1` or `0`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: a = "11", b = "1"
14+
Output: "100"
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: a = "1010", b = "1011"
21+
Output: "10101"
22+
```
23+
24+
25+
#### Solution
26+
27+
Language: **Java**
28+
29+
```java
30+
class Solution {
31+
   public String addBinary(String a, String b) {
32+
       char[] shortStr;
33+
       char[] longStr;
34+
       if (a.length() > b.length()) {
35+
           shortStr = b.toCharArray();
36+
           longStr = a.toCharArray();
37+
      } else {
38+
           shortStr = a.toCharArray();
39+
           longStr = b.toCharArray();
40+
      }
41+
       int plusNum = 0;
42+
       int i = longStr.length - 1;
43+
       int j = shortStr.length - 1;
44+
       do {
45+
           int sum = longStr[i] - '0' + plusNum;
46+
           if (j >= 0) {
47+
               sum += (shortStr[j] - '0');
48+
          }
49+
           longStr[i] = (char) (sum % 2 + '0');
50+
           plusNum = sum / 2;
51+
           i--;
52+
           j--;
53+
      } while (i >= 0);
54+
       if (plusNum > 0) {
55+
           return "1" + String.valueOf(longStr);
56+
      } else {
57+
           return String.valueOf(longStr);
58+
      }
59+
  }
60+
}
61+
```
62+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190719175928.png)

src/main/java/leetcode/_67_/Main.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode._67_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println(solution.addBinary("1010", "1011"));
10+
System.out.println(solution.addBinary("11", "1"));
11+
System.out.println(solution.addBinary("10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101", "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011"));
12+
}
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package leetcode._67_;
2+
3+
class Solution {
4+
public String addBinary(String a, String b) {
5+
char[] shortStr;
6+
char[] longStr;
7+
if (a.length() > b.length()) {
8+
shortStr = b.toCharArray();
9+
longStr = a.toCharArray();
10+
} else {
11+
shortStr = a.toCharArray();
12+
longStr = b.toCharArray();
13+
}
14+
int plusNum = 0;
15+
int i = longStr.length - 1;
16+
int j = shortStr.length - 1;
17+
do {
18+
int sum = longStr[i] - '0' + plusNum;
19+
if (j >= 0) {
20+
sum += (shortStr[j] - '0');
21+
}
22+
longStr[i] = (char) (sum % 2 + '0');
23+
plusNum = sum / 2;
24+
i--;
25+
j--;
26+
} while (i >= 0);
27+
if (plusNum > 0) {
28+
return "1" + String.valueOf(longStr);
29+
} else {
30+
return String.valueOf(longStr);
31+
}
32+
}
33+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
### [67\. Add Binary](https://leetcode.com/problems/add-binary/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given two binary strings, return their sum (also a binary string).
7+
8+
The input strings are both **non-empty** and contains only characters `1` or `0`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: a = "11", b = "1"
14+
Output: "100"
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: a = "1010", b = "1011"
21+
Output: "10101"
22+
```
23+
24+
25+
#### Solution
26+
27+
Language: **Java**
28+
29+
```java
30+
class Solution {
31+
   public String addBinary(String a, String b) {
32+
       char[] shortStr;
33+
       char[] longStr;
34+
       if (a.length() > b.length()) {
35+
           shortStr = b.toCharArray();
36+
           longStr = a.toCharArray();
37+
      } else {
38+
           shortStr = a.toCharArray();
39+
           longStr = b.toCharArray();
40+
      }
41+
       int plusNum = 0;
42+
       int i = longStr.length - 1;
43+
       int j = shortStr.length - 1;
44+
       do {
45+
           int sum = longStr[i] - '0' + plusNum;
46+
           if (j >= 0) {
47+
               sum += (shortStr[j] - '0');
48+
          }
49+
           longStr[i] = (char) (sum % 2 + '0');
50+
           plusNum = sum / 2;
51+
           i--;
52+
           j--;
53+
      } while (i >= 0);
54+
       if (plusNum > 0) {
55+
           return "1" + String.valueOf(longStr);
56+
      } else {
57+
           return String.valueOf(longStr);
58+
      }
59+
  }
60+
}
61+
```
62+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190719175928.png)

0 commit comments

Comments
 (0)