Add Binary Java


  • 0
    P
    class Solution {
        public String addBinary(String a, String b) {
            
            int i =a.length();
            int y = b.length();
            
            String answer = "";
            String addition="";
            
            int carry = 0;
            
            String one = "1";
            String zero = "0";
            
            while (i!=0&&y!=0){
                
                    if ((a.charAt(i-1)==one.charAt(0))&&(b.charAt(y-1)==one.charAt(0))){                
                    
                        if (carry==1){            
                        addition = "1";
                        }
                        else{
                        addition = "0";                  
                        }
                    carry = 1;
                    }
                
                    else if((a.charAt(i-1)==zero.charAt(0))&&(b.charAt(y-1)==zero.charAt(0))){
                    
                        if (carry==1){
                            addition = "1";
                            carry = 0;
                        }else{
                      
                    addition = "0";
                    }
                }else if (carry == 1){
                    addition = "0";
                    carry = 1;
                }
                else{
                    carry =0;
                    addition = "1";
                }
                i--;
                y--;
                answer = addition+answer;
                
                
            }
            if (i>0){
                while (i!=0){
                    if ((a.charAt(i-1)==one.charAt(0))){
                         
                        if (carry == 1){
                        carry = 1;
                        addition = "0";
                        }else{
                            addition="1";
                        }
                        
                    }else if ((a.charAt(i-1)==zero.charAt(0))){
                        if (carry==1){
                        carry =0;
                        addition="1";
                    }else 
                            addition = "0";
                    }
                     answer = addition+answer;
                 i--;
                }
                
            }
            
            if (y>0){
              
                while (y!=0){
                    if ((b.charAt(y-1)==one.charAt(0))){
                         
                        if (carry == 1){
                            
                        carry = 1;
                        addition = "0";
                        }else{
                            addition="1";
                            
                        }
                        
                    }else if ((b.charAt(y-1)==zero.charAt(0))){
                        
                        if (carry==1){
                            carry =0;
                        addition="1";
                    }else                        
                            addition = "0";
                    }
                     answer = addition+answer;
                 y--;
                }
            }
            
            if (carry==1){
                answer = "1"+answer;
            }
            return answer;
        }
    }

Log in to reply
 

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