The addition of the binary
After testing, it was found that an integer data overflow was not considered.
Leetcode often have some unexpected exceptions. I thought it was a very rich solution, and chose to: convert the string to int, add it with int, and then return to string. Because I hate using strings to carry them. But it overflowed. Can be improved, with BigInteger this class.
Put on my error code first.
ImportJava.io.*;ImportJava.util.*;Importjava.lang.*; Public classSolution { Public Static intToInt (String s) {intlen=s.length (); intI=0,r=0; Char[] c=S.tochararray (); for(i=0;i<len;i++) {R=r*2+c[i]-' 0 '; } returnR; } Public StaticString Tostr (intN) {String s=NewString (); intx=N; while(x!=0) { if(x%2==0) s=s+ "0"; Elses=s+ "1"; X=x/2; } intlen=s.length (); Char[] c=S.tochararray (); Char[] b=New Char[Len]; intI=0; for(i=0;i<len;i++) {B[i]=c[len-1-i]; } String R=string.valueof (b); returnR; } Public Staticstring Addbinary (String A, string b) {if(a==NULL) returnb; if(b==NULL) returnA; returnTostr (ToInt (a) +ToInt (b)); } Public Static voidMain (string[] args) {//System.out.println (Addbinary ("One", "1"));System.out.println (Addbinary ("1010", "1011")); }}
After changing to BigInteger version, the error is
A mouthful of old blood.
Although very angry but also still want to smile:)
Test:
And then I found something magical.
, the number I entered is the binary data that ends with 1, but this site actually gives me an even. The first thing I suspect is myself. Did I make a mistake myself?
Feedback the issue to open source China. In fact, I tested a number of large data, and the results were incorrect.
So, I started using binary manual rounding.
(difficult to be affected)
ImportJava.io.*;ImportJava.util.*;Importjava.lang.*; Public classSolution { Public Staticstring Reverse (string s) {intlen=s.length (); Char[] c=S.tochararray (); Char[] b=New Char[Len]; intI=0; for(i=0;i<len;i++) {B[i]=c[len-1-i]; } String R=string.valueof (b); returnR; } Public Staticstring Addbinary (String A, string b) {if(a==NULL|| A.equals ("0")) returnb; if(b==NULL|| B.equals ("0")) returnA; intlen1=a.length (); intLen2=b.length (); intLen; if(len1<len2) Len=len1; ElseLen=Len2; inti; Char[] Ca=A.tochararray (); Char[] cb=B.tochararray (); String R=NewString (); Boolean Flag=false; for(i=len-1;i>=0;i--) { if(ca[i]== ' 1 ' &&cb[i]== ' 1 ') { if(flag==true) R=r+ "1"; ElseR=r+ "0"; Flag=true; } if(ca[i]== ' 1 ' &&cb[i]== ' 0 ') { if(flag==true) {R=r+ "0"; Flag=true; } Else{R=r+ "1"; Flag=false; } } if(ca[i]== ' 0 ' &&cb[i]== ' 1 ') { if(flag==true) {R=r+ "0"; Flag=true; } Else{R=r+ "1"; Flag=false; } } if(ca[i]== ' 0 ' &&cb[i]== ' 0 ') { if(flag==true) {R=r+ "1"; } Else{R=r+ "0"; } Flag=false; } } if(len1>len2) { for(i=len1-len-1;i>=0;i--) { if(ca[i]== ' 0 ' &&flag==true) {R=r+ "1"; Flag=false; } if(ca[i]== ' 0 ' &&flag==false) {R=r+ "0"; Flag=false; } if(ca[i]== ' 1 ' &&flag==true) {R=r+ "0"; Flag=true; } if(ca[i]== ' 1 ' &&flag==false) {R=r+ "1"; Flag=false; } } } Else { for(i=len2-len-1;i>=0;i--) { if(cb[i]== ' 0 ' &&flag==true) {R=r+ "1"; Flag=false; } if(cb[i]== ' 0 ' &&flag==false) {R=r+ "0"; Flag=false; } if(cb[i]== ' 1 ' &&flag==true) {R=r+ "0"; Flag=true; } if(cb[i]== ' 1 ' &&flag==false) {R=r+ "1"; Flag=false; } } } if(flag==true) R=r+ "1"; String s=reverse (R); returns; } Public Static voidMain (string[] args) {//System.out.println (Addbinary ("One", "1"));System.out.println (Addbinary ("1111", "1000")); }}
Leetcode Add Binary (Easy)/java