67. Add Binary

原创 2017年01月02日 22:49:40

Given two binary strings, return their sum (also a binary string).

For example,
a = “11”
b = “1”
Return “100”.

class Solution {
public:
    string addBinary(string a, string b) {
        int c = 0, quo = 0, i, j;
        string ans;
        for(i = a.length() - 1, j = b.length() - 1; i >= 0 || j >= 0; --i, --j){
            if(i >= 0 && j >= 0){
                quo = (a[i] - '0' + b[j] - '0' + c) % 2;
                c = (a[i] - '0' + b[j] - '0' + c) / 2;
            } else if(i >= 0){
                quo = (a[i] - '0' + c) % 2;
                c = (a[i] - '0' + c) / 2;
            } else if(j >= 0){
                quo = (b[j] - '0' + c) % 2;
                c = (b[j] - '0' + c) / 2;
            }
            ans.insert(ans.begin(), (quo + '0'));
        }

        if(c != 0)
            ans.insert(ans.begin(), (c + '0'));
        return ans;
    }
};

二刷

class Solution {
public:
    string addBinary(string a, string b) {
        string res;
        int i, j, carry = 0;
        for(i = a.length() - 1, j = b.length() - 1; i >= 0 && j >= 0; --i, --j){
            res.push_back((a[i] - '0' + b[j] - '0' + carry) % 2 + '0');
            carry = (a[i] - '0' + b[j] - '0' + carry) / 2;
        }
        while(i >= 0){
            res.push_back((a[i] - '0' + carry) % 2 + '0');
            carry = (a[i] - '0' + carry) / 2;
            --i;
        }
        while(j >= 0){
            res.push_back((b[j] - '0' + carry) % 2 + '0');
            carry = (b[j] - '0' + carry) /  2;
            --j;
        }
        if(carry) res.push_back('1');

        string rev;
        int len = res.length();
        for(i = len - 1; i >= 0; --i){
            rev.push_back(res[i]);
        }
        return rev;
    }
};

相关文章推荐

LeetCode 67 — Add Binary(C++ Java Python)

题目:http://oj.leetcode.com/problems/add-binary/ Given two binary strings, return their sum (also a bi...

LeetCode(67) Add Binary

题目Given two binary strings, return their sum (also a binary string).For example, a = “11” b = “1” ...
  • fly_yr
  • fly_yr
  • 2015年08月19日 20:58
  • 1687

67. Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1...

LeetCode-67 Add Binary(二进制数相加)

LeetCode-67 Add Binary(二进制数相加) Given two binary strings, return their sum (also a binary string). ...

LeetCode 67 Add Binary(二进制相加)(*)

翻译给定两个二进制字符串,返回它们的和(也是二进制字符串)。例如, a = "11" b = "1" 返回 "100".原文Given two binary strings, return their...
  • NoMasp
  • NoMasp
  • 2016年02月02日 13:49
  • 1942

LeetCode笔记:67. Add Binary

模拟两个字符串二进制数的加法

Add Binary - LeetCode 67

题目描述:Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "...
  • bu_min
  • bu_min
  • 2015年04月24日 22:05
  • 259

LeetCode 67. Add Binary(二进制加)

原题网址:https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (also a ...
  • jmspan
  • jmspan
  • 2016年05月22日 00:04
  • 274

LeetCode-67. Add Binary

问题描述Given two binary strings, return their sum (also a binary string). For example, a = “11” b = ...

leetcode--67. Add Binary

leetcode
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:67. Add Binary
举报原因:
原因补充:

(最多只允许输入30个字)