Add Binary

简介: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路:逐位相加,进位保留在和的下一位中。

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

For example,
a = "11"
b = "1"
Return "100".

思路:逐位相加,进位保留在和的下一位中。

 

C++实现代码:

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

class Solution
{
public:
    string addBinary(string a, string b)
    {
        int size1=a.size()-1;
        int size2=b.size()-1;
        int size=max(size1,size2);
        string digits(size+1,'0');
        while(size1>=0&&size2>=0)
        {
            if((a[size1]=='1')&&(b[size2]=='1'))
            {
                digits[size-1]='1';
                if(size1==0&&size2==0)
                    digits.insert(digits.begin(),'1');
                size1--;
                size2--;
                size--;
            }
            else if(((a[size1]=='1')||(b[size2]=='1'))&&(digits[size]=='0'))
            {
                digits[size]='1';
                size1--;
                size2--;
                size--;
            }
            else if(((a[size1]=='1')||(b[size2]=='1'))&&(digits[size]=='1'))
            {
                digits[size]='0';
                digits[size-1]='1';
                if(size1==0&&size2==0)
                    digits.insert(digits.begin(),'1');
                size1--;
                size2--;
                size--;
            }
            else
            {
                digits[size]=digits[size];
                size1--;
                size2--;
                size--;
            }
        }
        cout<<digits<<endl;
        if(size1>=0)
        {
            while(size1>=0&&size>=0)
            {
                if((digits[size]=='1')&&(a[size1]=='1'))
                {
                    digits[size]='0';
                    digits[size-1]='1';
                    if(size==0)
                        digits.insert(digits.begin(),'1');
                    size1--;
                    size--;
                }
                else if((digits[size]=='1')&&(a[size1]=='0'))
                {
                    size1--;
                    size--;
                }
                else
                {
                    digits[size]=a[size1];
                    size--;
                    size1--;
                }
            }
        }
        if(size2>=0)
        {
            while(size2>=0&&size>=0)
            {
                if((digits[size]=='1')&&(b[size2]=='1'))
                {
                    digits[size]='0';
                    digits[size-1]='1';
                    //最前面两个数相加需要进位的时候,在前面插入1
                    if(size==0)
                        digits.insert(digits.begin(),'1');
                    size2--;
                    size--;
                }
                else if((digits[size]=='1')&&(b[size2]=='0'))
                {
                    size2--;
                    size--;
                }
                else
                {
                    digits[size]=b[size2];
                    size--;
                    size2--;
                }
            }
        }
        return digits;
    }
};
int main()
{
    string b="100";
    string a="110010";
    Solution s;
    cout<<"digits: "<<s.addBinary(a,b)<<endl;
}

 只能想到这么麻烦的方法,哎。。。

    string addBinary(string a, string b) {
        if(a.empty())
            return b;
        if(b.empty())
            return a;
        int carry=0;
        int i=a.size()-1;
        int j=b.size()-1;
        string res;
        int sum=0;
        while(i>=0&&j>=0)
        {
            sum=a[i--]-'0'+b[j--]-'0'+carry;
            if(sum>=2)
            {
                char c=sum%2+'0';
                res=c+res;
                carry=1;
            }
            else
            {
                char c=sum+'0';
                res=c+res;
                carry=0;
            }
        }
        while(i>=0)
        {
            sum=carry+a[i--]-'0';
            if(sum>=2)
            {
                char c=sum%2+'0';
                res=c+res;
                carry=sum/2;
            }
            else
            {
                char c=sum+'0';
                res=c+res;
                carry=0;
            }
        }
        while(j>=0)
        {
            sum=carry+b[j--]-'0';
            if(sum>=2)
            {
                char c=sum%2+'0';
                res=c+res;
                carry=sum/2;
            }
            else
            {
                char c=sum+'0';
                res=c+res;
                carry=0;
            }
        }
        if(carry==1)
            res.insert(0,"1");
        return res;
    }

 

相关文章
|
5月前
|
算法 数据库 索引
Binary Search
二分查找(Binary Search)是一种在有序数组中查找目标值的算法。它的基本思想是将数组分成两半,判断目标值是否在左半部分或右半部分,然后递归地在相应的半部分中查找。这个过程不断重复,直到找到目标值或者确定目标值不存在为止。二分查找的时间复杂度为 O(logn),其中 n 是数组的长度。
37 0
add file in debian/source/include-binaries if you want to store the modified binary in the debian
add file in debian/source/include-binaries if you want to store the modified binary in the debian
197 0
LeetCode 67. Add Binary
给定两个二进制字符串,返回它们的总和(也是二进制字符串)。 输入字符串都是非空的,只包含字符1或0。
53 0
LeetCode 67. Add Binary
【1064】Complete Binary Search Tree (30 分)
【1064】Complete Binary Search Tree (30 分) 【1064】Complete Binary Search Tree (30 分)
80 0
How to add extension field to report
How to add extension field to report
106 0
1110. Complete Binary Tree (25)
#include #include #include #include using namespace std; struct node { int ad, l, r; }; vector visited(20, ...
957 0
|
机器学习/深度学习
1064. Complete Binary Search Tree (30)
#include #include #include using namespace std; const int maxn = 1001; vector num(maxn), cbt(maxn); int n, c...
825 0