67. Add Binary

原创 2016年05月31日 10:04:51

67. Add Binary

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

For example,
a = "11"
b = "1"
Return "100".
Analysis:
相当于自己写一个加法运算,主要是处理好进位与当前位之间的关系。还有注意空字符串。
本题跟66. Plus One 比较类似。
Source Code(C++):

#include <iostream>
#include <string>
using namespace std;

class Solution {
public:
    string addBinary(string a, string b) {
        char digit_overflow='0';
        char current_bit='0';
        int a_index=a.size()-1, b_index=b.size()-1;
        string sum;
        while(a_index>=0 && b_index>=0){
            current_bit=(a.at(a_index)-'0'+b.at(b_index)-'0'+digit_overflow-'0')%2 + '0';
            sum.insert(0, 1, current_bit);
            digit_overflow = (a.at(a_index)-'0'+b.at(b_index)-'0'+digit_overflow-'0')/2 + '0';          
            a_index--;
            b_index--;
        }
        while(a_index>=0){
            current_bit=(a.at(a_index)-'0'+digit_overflow-'0')%2 + '0';
            sum.insert(0, 1, current_bit);
            digit_overflow = (a.at(a_index)-'0'+digit_overflow-'0')/2 + '0';        
            a_index--;
        }
        while(b_index>=0){
            current_bit=(b.at(b_index)-'0'+digit_overflow-'0')%2 + '0';
            sum.insert(0, 1, current_bit);
            digit_overflow = (b.at(b_index)-'0'+digit_overflow-'0')/2 + '0';        
            b_index--;
        }
        if (digit_overflow > '0') {
            sum.insert(0, 1, digit_overflow);
        }
        return sum;
    }
};


int main() {
    Solution sol;
    cout << sol.addBinary("", "");
    cout << sol.addBinary("101", "1");
    cout << sol.addBinary("10", "11");
    return 0;
}
版权声明:转载请注明出处,谢谢。

相关文章推荐

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
  • 1704

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
  • 1962

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
  • 262

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
  • 280

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个字)