给定两个二进制字符串,返回他们的和(用二进制表示)。
案例:
a = "11"
b = "1"
返回 "100" 。
详见:https://leetcode.com/problems/add-binary/description/

Java实现:

class Solution {
    public String addBinary(String a, String b) {
        String s="";
        int c=0;
        int i=a.length()-1;
        int j=b.length()-1;
        while(i>=0||j>=0||c==1){
            c+=i>=0?a.charAt(i)-'0':0;
            c+=j>=0?b.charAt(j)-'0':0;
            s=(char)(c%2+'0')+s;
            c/=2;
            --i;
            --j;
        }
        return s;
    }
}

 

相关文章:

  • 2022-02-15
  • 2022-01-01
  • 2021-12-12
  • 2021-07-29
  • 2022-02-23
  • 2021-11-12
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2021-07-06
  • 2021-07-15
  • 2021-09-12
  • 2022-12-23
相关资源
相似解决方案