Leetcode Add Binary

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

class Solution {
public:
string addBinary(string a, string b) {
int carry = , alen = a.length(),blen = b.length();
string res="";
if(alen > blen) {b=string(alen-blen,'')+b;blen = alen;}
if(alen < blen) {a=string(blen-alen,'')+a;alen = blen;}
for(int i = alen-; i>= ; -- i){
int num =(a[i]-'')+(b[i]-'')+carry;
carry = ;
if(num >= ) {num-=;carry=;}
res+=''+num;
}
if(carry) res+='';
reverse(res.begin(),res.end());
return res;
}
};
上一篇:ORA-01033: ORACLE initialization or shutdown in progress --手动删除表空间 DBF 后无法登陆问题


下一篇:python之--条件判断和循环