leetcode-67.二进制求和

Points

题意

算法

code

 class Solution {
public:
string addBinary(string a, string b) {
int shortLen, dvalue;
string ans, addStr;
int thisBit, nextBit, t;
if(a.length() >= b.length())
{
shortLen = b.length();
ans = a;
addStr = b;
dvalue = a.length() - b.length();
}
else
{
shortLen = a.length();
ans = b;
addStr = a;
dvalue = b.length() - a.length();
}
for(int i=shortLen-; i>=; i--)
{
t = i + dvalue;
ans[t] += (addStr[i]-'');
thisBit = (ans[t]-'')%;
nextBit = (ans[t]-'')/;
if((ans[t] - '') >= )
{
ans[t] = (thisBit + '');
if(t == )
{
ans.insert(, , (nextBit+''));
break;
}
else
{
ans[t-] += nextBit;
}
}
}
t--;
if(t >= )
{
while((ans[t] - '') >= && (t >= ))
{
thisBit = (ans[t]-'')%;
nextBit = (ans[t]-'')/;
ans[t] = (thisBit + '');
if(t == )
{
ans.insert(, , (nextBit+''));
break;
}
else
{
ans[t-] += nextBit;
t--;
}
}
}
return ans;
}
};
05-11 08:34