my solution


  • 0
    W

    public class Solution {
    /**
    * @param a a number
    * @param b a number
    * @return the result
    */
    public String addBinary(String a, String b) {
    // Write your code here
    if (a == null || b == null) {
    return null;
    }
    return parseInt2String(parseString2(a) + parseString2(b));
    }

     public static int parseString2(String str1){
        char c = 0;
        int sum = 0;
        for (int i = 0; i < str1.length(); i++) {
            if (str1.charAt(i) == '1'){
                sum += 1<<(str1.length()-1-i);
            }
        }
        return sum;
    }
    
    public static String parseInt2String(int a){
        String str = "";
        if (a == 0)
            return "0";
        while (a != 0){
            str += a % 2;
            a = a/2;
        }
        String str2 = "";
        int i = str.length();
        while (i > 0 ){
            str2+=str.charAt(i-1);
            i--;
        }
        return str2;
    }
    

    }


Log in to reply
 

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