3ms java solution beats 90%+


  • 1
    X
    public String addBinary(String a, String b) {
        if(a.length()<b.length()){
    		String temp = a;
    		a = b;
    		b = temp ;
    	}
        
       char[] array = new char[a.length()] ;
       for(int i=0; i<a.length(); i++){
    	   array[i] = a.charAt(i) ;
       }
       
       int increase = 0 ;
       int i = 0 ;
       int j = 0 ;
       for(i=array.length-1 , j=b.length()-1; i>=0 && j>=0; i--,j--){
    	   if(array[i]=='1' && b.charAt(j)=='1'){//	11
    		   if(increase==1){
    			   array[i] = '1' ;
    		   }else{
    			   array[i] = '0' ;
    			   increase = 1 ;
    		   }
    	   }else if((array[i]=='1' ^ b.charAt(j)=='1')){// 10 or 01
    		   if(increase==1){
    			   increase = 1 ;
    			   array[i] = '0' ;
    		   }else{
    			   array[i] = '1' ;
    		   }
    	   }else{//	00
    		   if(increase==1){
    			   increase = 0 ;
    			   array[i] = '1' ;
    		   }else{
    			   array[i] = '0' ;
    		   }
    	   }
       }
       
       
       if(increase==1){
    	  while(i>=0 && array[i]=='1'){
    		  array[i] = '0' ;
    		  i-- ;
    	  }
    	  if(i>0){
    		  array[i] = '1' ;
    		  return new String(array) ;
    	  }else{
    		  return "1"+new String(array) ;
    	  }
       }
       
    	return new String(array) ; 
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.