Given binary strings, return their sum (also a binary string).
For example,
A = "11"
b = "1"
Return "100"
.
This problem is very simple, not much to say, the code on the
classSolution { Public: stringAddbinary (stringAstringb) {intLengtha =a.length (); intLENGTHB =b.length (); intIndexa = A.length ()-1; intIndexb = B.length ()-1; intres =0;//Set Rounding stringresult; Map<Char,int>mapping; mapping['1'] =1; mapping['0'] =0; intvalue; while(indexa>=0|| indexb>=0) { if(Indexa <0) Value=0+ Mapping[b[indexb]] +Res; Else if(Indexb <0) Value= Mapping[a[indexa]] +0+Res; Elsevalue= Mapping[a[indexa]] + MAPPING[B[INDEXB]] +Res; if(Value >=2) {res=1; Value%=2; } Else{res=0; } result.push_back (Value+'0'-0); Indexa--; Indexb--; } if(res = =1) Result.push_back (res+'0'-0); Reverse (Result.begin (), Result.end ()); returnresult; }};
Happyleetcode37:add Binary