67. Add Binary

https://leetcode.com/problems/add-binary/description/

解题思路:

  1. 从最低位开始相加,然后考虑进位

代码:
class Solution {
public String addBinary(String a, String b) {
String s = "";
int i = a.length() - 1, j = b.length() - 1, c = 0;
while(i >= 0 || j >= 0 || c == 1){
c += i >= 0 ? a.charAt(i--) - '0' : 0;
c += j >= 0 ? b.charAt(j--) - '0' : 0;
s = c % 2 + s;
c /= 2;
}
return s;
}
}

你可能感兴趣的:(67. Add Binary)