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

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

leetcode--67. Add Binary

leetcode

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

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