Tuesday, December 27, 2016

Leetcode/F家 --67.Add Binary (String)

67.Add Binary(String)
  • Difficulty: Easy
https://leetcode.com/problems/add-binary/
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".

思路: String相加的解法,如进位(inc =1),不然s设回0,sb加的值要%2, i j condition
注意点: numA = charA -'0' (char转成int)
关键字: char to int, StringBuilder, mod


public String addBinary(String a, String b) {
        int i = a.length() - 1, j = b.length() - 1;
        StringBuilder res = new StringBuilder();
        int inc = 0;
        while(i >= 0 || j >= 0){
            int sum = inc;
            inc = 0;//set back inc
            if(i >= 0)sum += a.charAt(i--) - '0';
            if(j >= 0)sum += b.charAt(j--) - '0';
            if(sum >= 2)inc = 1;
            res.append(sum%2);//only have 3 2 1 0
        }
        if(inc == 1)res.append(inc);
        return res.reverse().toString();
    }

No comments:

Post a Comment