-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add Binary.cpp
55 lines (54 loc) · 1.34 KB
/
Add Binary.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
string addBinary(string a, string b) {
int lenA=a.length()-1;
int lenB=b.length()-1;
string addAB;
while(lenA>=0&&lenB>=0)//先求和然后放入新的String里面
{
addAB+=(a[lenA]-'0')+(b[lenB]-'0')+'0';
lenA--;
lenB--;
}
while(lenA>=0)//A还没结束
{
addAB+=a[lenA];
lenA--;
}
while(lenB>=0)//B还没结束
{
addAB+=b[lenB];
lenB--;
}
int lenAB=addAB.length()-1;//此时addAB中放的是倒序的。
int i=0;
int temp=0;
while(i<=lenAB)//开始进位
{
temp=addAB[i]-'0';
if(temp>=2)
{
addAB[i]=temp%2+'0';//先改自己的
if(i==lenAB)//如果是最后一个那还要进位的。
{
addAB+=(temp/2+'0');
break;
}
addAB[i+1]=((addAB[i+1]-'0')+temp/2)+'0';
}
i++;
}
char t;
i=0;
lenAB=addAB.length()-1;
while(i<=lenAB)
{
t=addAB[i];
addAB[i]=addAB[lenAB];
addAB[lenAB]=t;
i++;
lenAB--;
}
return addAB;
}
};