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

题目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
  • 1566

leetcode--67. Add Binary

leetcode

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

67. Add Binary

**题目: Given two binary strings, return their sum (also a binary string). For example, a = “11” b...

67.[LeetCode]Add Binary

一开始我是有两个思路的,第一个是直接在字符位来计算,第二个是先转化为int整型,然后计算,再转换为String,这个想法 明显忽略掉 超过32的大数的可能。设计算法要考虑可能的范围我的算法 : 耗时 ...

【Leetcode】67. Add Binary

注: string转int:atoi(str.c_str()) int转string:to_string(num) int,long,long long取值范围: unsigned int 0...

LeetCode 67. Add Binary

67. Add Binary一、问题描述 Given two binary strings, return their sum (also a binary string). 二、输入输出For ...

LeetCode-67. Add Binary

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

Leetcode 67. Add Binary

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

[LeetCode]67.Add Binary

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

Add Binary - LeetCode 67

题目描述:Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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