1 public class Solution { 2 public String addBinary(String a, String b) { 3 char[] aa=a.toCharArray(); 4 char[] bb=b.toCharArray(); 5 6 int size=aa.length>=bb.length?aa.length:bb.length; 7 int[] mm=new int[size]; 8 int c=0; 9 int i=aa.length-1,j=bb.length-1,k=size-1; 10 for(;i>=0&&j>=0;i--,j--,k--) 11 { 12 mm[k]=(aa[i]-'0'+bb[j]-'0'+c)%2; 13 14 if(aa[i]-'0'+bb[j]-'0'+c>=2) c=1; 15 else c=0; 16 } 17 18 while(i>=0) 19 { 20 mm[k]=(aa[i]-'0'+c)%2; 21 if(aa[i]-'0'+c>=2) c=1; 22 else c=0; 23 24 k--;i--; 25 26 } 27 while(j>=0) 28 { 29 mm[k]=(bb[j]-'0'+c)%2; 30 31 if(bb[j]-'0'+c>=2) c=1; 32 else c=0; 33 34 k--;j--; 35 } 36 37 String s=""; 38 if(c==1) 39 s+=String.valueOf(c); 40 41 for(int n=0;n<mm.length;n++) 42 s=s+String.valueOf(mm[n]); 43 44 45 return s; 46 } 47 }