-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathL0067_AddBinary.java
68 lines (60 loc) · 2.03 KB
/
L0067_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
57
58
59
60
61
62
63
64
65
66
67
68
/**
* https://leetcode.cn/problems/add-binary/
*
* 给你两个二进制字符串 a 和 b ,以二进制字符串的形式返回它们的和。
*
* 示例 1:
* 输入:a = "11", b = "1"
* 输出:"100"
*
* 示例 2:
* 输入:a = "1010", b = "1011"
* 输出:"10101"
*
* 提示:
* 1 <= a.length, b.length <= 104
* a 和 b 仅由字符 '0' 或 '1' 组成
* 字符串如果不是 "0" ,就不含前导零
*/
public class L0067_AddBinary {
// 从右往左遍历两个字符串,按位相加并处理进位
public String addBinary(String a, String b) {
// 结果字符串
StringBuilder result = new StringBuilder();
// 进位
int carry = 0;
// 从右往左遍历两个字符串
int i = a.length() - 1;
int j = b.length() - 1;
// 只要还有数字需要相加,就继续循环
while (i >= 0 || j >= 0 || carry > 0) {
// 获取当前位的值,如果已经遍历完了字符串,就用 0 代替
int digitA = i >= 0 ? a.charAt(i) - '0' : 0;
int digitB = j >= 0 ? b.charAt(j) - '0' : 0;
// 计算当前位的和
int sum = digitA + digitB + carry;
// 更新进位
carry = sum / 2;
// 当前位的结果
result.insert(0, sum % 2);
// 移动指针
i--;
j--;
}
return result.toString();
}
public static void main(String[] args) {
L0067_AddBinary solution = new L0067_AddBinary();
// 测试用例 1
String a1 = "11";
String b1 = "1";
System.out.println("Input: a = \"" + a1 + "\", b = \"" + b1 + "\"");
System.out.println("Output: \"" + solution.addBinary(a1, b1) + "\"");
System.out.println();
// 测试用例 2
String a2 = "1010";
String b2 = "1011";
System.out.println("Input: a = \"" + a2 + "\", b = \"" + b2 + "\"");
System.out.println("Output: \"" + solution.addBinary(a2, b2) + "\"");
}
}