题目链接https://leetcode.cn/problems/add-binary/description/
找几个字符串练习一下.
class Solution {
public String addBinary(String a, String b) {
StringBuilder sbt= new StringBuilder();
int flag=0;//进位标志
int i=a.length()-1,j=b.length()-1;
while(i>=0 || j>=0){
int ca=0;
int ba=0;
if(i>=0){
if(a.charAt(i) == '1'){
ca=1;
}else{
ca=0;
}
}
if(j>=0){
if(b.charAt(j) == '1'){
ba=1;
}else{
ba=0;
}
}
sbt.append((ca+ba+flag)%2);
if(ca+ba+flag>1){
flag=1;
}else{
flag=0;
}
i--;j--;
}
if(flag==1){
sbt.append(flag);
}
return sbt.reverse().toString();
}
}