Java solution


  • 0
    Z

    '''public class Solution {
    public String addBinary(String a, String b) {

        String resultB="", resultF="";
        int unitR;
        int get=0;
        if(a.length()>b.length()){
            String c=b;
            b=a;
            a=c;
        }
        
        for(int i=a.length()-1;i>-1;i--){
            unitR=get+Character.getNumericValue(a.charAt(i))+Character.getNumericValue(b.charAt(i+b.length()-a.length()));
            resultB+=Integer.toString(unitR%2);
            System.out.println(unitR);
            if(unitR>=2){ 
                get=1;
            }
            else{
                get=0;
            }
            
        }
        for(int i=b.length()-a.length()-1;i>-1;i--){
            unitR=get+Character.getNumericValue(b.charAt(i));
            resultB+=Integer.toString(unitR%2);
            if(unitR>=2){ 
                get=1;
            }
            else{
                get=0;
            }
        }
        if(get==1){
            resultB+="1";
        }
        for(int i=resultB.length()-1;i>-1;i--){
            resultF+=resultB.charAt(i);
        }
       
        return resultF;
    }
    

    }'''


Log in to reply
 

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