Question:
Given binary strings, return their sum (also a binary string).
For example,
A ="11"
b ="1"
Return "100" .
Analysis:
Idea one: First convert the binary string into decimal, then add the decimal, and then return to the binary. Cons: saving variables with int or long data is prone to overflow and does not result in correct results.
Thought two: Directly with binary Add, every two into one.
Answer:
1:2 binary to decimal and then to binary (overflow/(ㄒoㄒ)/~~).
Public classSolution { Publicstring Addbinary (String A, string b) {if(A.equals ("") && b.equals ("")) return"0"; if(A = =NULL|| A.equals ("") | | A.equals ("0")) returnb; if(b = =NULL|| B.equals ("") | | B.equals ("0")) returnA; Char[] A1 =A.tochararray (); Char[] B1 =B.tochararray (); LongV1 =Chartoint (A1); LongV2 =Chartoint (B1); Longsum = v1 +v2; String Res=inttobin (sum); returnRes; } Private StaticString Inttobin (Longsum) { //TODO auto-generated Method Stublist<long> L =NewArraylist<long>(); while(Sum > 0) { Longt = sum% 2; L.add (t); Sum= SUM/2; } String Res= ""; for(intI=l.size ()-1; i>=0; i--) {String T=L.get (i). toString (); Res= Res +T; } returnRes; } Private Static intChartoint (Char[] A1) { //TODO auto-generated Method Stub intval = 0; for(inti=0; i<a1.length; i++) {Val= val * 2 + (A1[i]-48); } returnVal; }}
Two. Add directly with Binary
Public classSolution { Publicstring Addbinary (String A, string b) {if(A.equals ("") && b.equals ("")) return"0"; if(A = =NULL|| A.equals ("") | | A.equals ("0")) returnb; if(b = =NULL|| B.equals ("") | | B.equals ("0")) returnA; Char[] A1 =A.tochararray (); Char[] B1 =B.tochararray (); String Res= ""; inti = A1.length-1; intj = B1.length-1; BooleanFlag =false; while(I>=0 && j>=0) { if(A1[i] = = ' 1 ' && b1[j] = = ' 1 ' && flag = =false) {res+ = ' 0 '; Flag=true; I--; J--; Continue; } if(A1[i] = = ' 1 ' && b1[j] = = ' 1 ' && flag = =true) {res+ = ' 1 '; Flag=true; I--; J--; Continue; } if(A1[i] = = ' 0 ' && b1[j] = = ' 1 ' && flag = =true|| A1[i] = = ' 1 ' && b1[j] = = ' 0 ' && flag = =true) {res+ = ' 0 '; Flag=true; I--; J--; Continue; } if(A1[i] = = ' 0 ' && b1[j] = = ' 1 ' && flag = =false|| A1[i] = = ' 1 ' && b1[j] = = ' 0 ' && flag = =false) {res+ = ' 1 '; Flag=false; I--; J--; Continue; } if(A1[i] = = ' 0 ' && b1[j] = = ' 0 ' && flag = =true) {res+ = ' 1 '; Flag=false; I--; J--; Continue; } if(A1[i] = = ' 0 ' && b1[j] = = ' 0 ' && flag = =false) {res+ = ' 0 '; Flag=false; I--; J--; Continue; } } while(i>=0) { if(A1[i] = = ' 0 ' && flag = =true) {res+ = ' 1 '; Flag=false; } Else if(A1[i] = = ' 0 ' && flag = =false) {res+ = ' 0 '; Flag=false; } Else if(A1[i] = = ' 1 ' && flag = =true) {res+ = ' 0 '; Flag=true; } Else if(A1[i] = = ' 1 ' && flag = =false) {res+ = ' 1 '; Flag=false; } I--; } while(j>=0) { if(B1[j] = = ' 0 ' && flag = =true) {res+ = ' 1 '; Flag=false; } Else if(B1[j] = = ' 0 ' && flag = =false) {res+ = ' 0 '; Flag=false; } Else if(B1[j] = = ' 1 ' && flag = =true) {res+ = ' 0 '; Flag=true; } Else if(B1[j] = = ' 1 ' && flag = =false) {res+ = ' 1 '; Flag=false; } J--; } if(Flag = =true) Res+ = ' 1 ';
StringBuffer SB=NewStringBuffer (RES); Sb.reverse (); returnsb.tostring (); }}
Leetcode--Add Binary