Leetcode Algorithms-67.Add Binary

问题地址

Add Binary - LeetCode

问题描述

0.jpg

解法

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
public class Solution {

public String addBinary(String a, String b) {
if (null == a || a.length() == 0) return b;
if (null == b || b.length() == 0) return a;
StringBuilder sb = new StringBuilder();
int ia = a.length() - 1;
int ib = b.length() - 1;
boolean mark = false;
while (ia >= 0 || ib >= 0) {
char ta = ia >= 0 ? a.charAt(ia) : '0';
char tb = ib >= 0 ? b.charAt(ib) : '0';
if (ta == '0' && tb == '0') {
if (mark) sb.append('1');
else sb.append('0');
mark = false;
} else if (ta == '1' && tb == '1') {
if (mark) sb.append('1');
else sb.append('0');
mark = true;
} else {
if (mark) sb.append('0');
else sb.append('1');
}
ia--;
ib--;
}
if (mark) sb.append('1');
return sb.reverse().toString();
}
}
0%