It's quite strange,is there anywrong with my expression? some body could help?


  • 0
    F
    public String addBinary(String a, String b) {
        int len_a = a.length()-1;
        int len_b = b.length()-1;
        int carry = 0;
        StringBuilder sb = new StringBuilder();
        while(len_a>-1||len_b>-1||carry==1){
            int digit_a = (len_a>-1) ? a.charAt(len_a--) : 0; 
      //something wrong with this line? 
       // variable digit_a = 48, not   a.charAt(len_a--) 's value,i dont know why?
            int digit_b = (len_b>-1) ? a.charAt(len_b--) : 0;
            int sum = digit_a ^ digit_b ^ carry;
            sb.append(sum);
            carry = (digit_a + digit_b + carry >= 2)? 1:0;
        }
        return sb.reverse().toString();
    }

Log in to reply
 

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