Input:"0", "1" , my code is correct in eclipse ("1") but error in OJ ("101" )


  • 1
    L
    public class Solution 
    {
      
                
        public String addBinary(String a,String b)
        {
            String temp = "";
            String c = null;
            if(a.equals("0")&&b.equals("0")) return "0";
            if(a.length()==0 && b.length()==0) {return "";}
            else if(a.length()==0){return b;}
            else if(b.length()==0){return a;}
            
            else
            {
                if(a.length()>b.length())
                {
                    for(int i=0;i<a.length()-b.length();i++)
                    {
                        temp+="0";
                        
                    
                    }
                    temp+=b;
            //      System.out.println("@"+temp);
                    c=cal(a,temp);
                
                
                }   
            
                else if(b.length()>a.length())
                {
                    for(int i=0;i<b.length()-a.length();i++)
                    {
                        temp+="0";
                    
                    }
                    temp+=a;
                    c=cal(temp,b);
                }
                else
                {
                    c=cal(a,b);
                
                }
            }
            
            String[] cc=c.split("");
            
            String answer="";
            
            for(int i=cc.length-1;i>=0;i--)
            {
                answer+=cc[i];
                
            }
            return answer;
            
            
        }
        public  String cal(String aa,String bb)
        {
            String[] a=aa.split("");
            String[] b=bb.split("");
            
            String c="";
            int ia,ib,ic,iadd;
            ia=ib=ic=iadd=0;
            
            
            
            for(int i=a.length-1;i>=0;i--)
            {
                if(a[i].equals("0")){ia=0;} else{ia=1;}
                if(b[i].equals("0")){ib=0;} else{ib=1;}
                iadd=ia+ib+ic;
                
                    if(iadd==0) {c+="0"; ic=0;}
                    else if(iadd==1) {c+="1"; ic=0;}
                    else if(iadd==2) {c+="0"; ic=1;}
                    else if(iadd==3) {c+="1"; ic=1;}
        
                    
            }
            if(ic==1) {c+="1";}
            return c;
            
            
            
        }

  • 2
    C

    Lachao,

    Just tested in Eclipse. The code was not right in cal("0", "1");

    If you define a variable for the string length, you will catch.

    int l = a.length; // l == 2

    then the second loop:

    a[0] is "", you set it with ia = 1;
    b[0] is "", you set it as ib = 1;
    So you iadd = 2, then ic = 1;

    Your result will be "101" instead of "1".

    Just change your program as following:

            if(a[i].equals("1")){ia=1;} else{ia=0;}
            if(b[i].equals("1")){ib=1;} else{ib=0;}
    

    You problem will be resolved.

    Good luck,
    Peng


  • 0
    L

    thanks for your answer:), it really help me to pass the "101" output problem, but it face another wrong output "01" with the same test case "0","1". (the output is also correct in my eclipse code)


Log in to reply
 

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