码迷,mamicode.com
首页 > 其他好文 > 详细

Add Binary

时间:2015-07-02 10:13:28      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:

有三点收获:
第一:string类型的变量可以直接相互赋值。
第二:begin()-1在本地VS2012会出现运行时错误迭代器错误,而在leetcode在线提交时并没有出现错误,运行通过。
第三:反转string类型变量,直接如下使用即可:
reverse(reversedResult.begin(), reversedResult.end());头文件并不需要添加。
下面是程序代码

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

class Solution {
public:
    string addBinary(string a, string b) {
        string largeStr,smallStr;
        if(a.size() >= b.size()){
            largeStr = a;
            smallStr = b;
        }else {
            largeStr = b;
            smallStr = a;
        }

        int lengthCommon = smallStr.size();
        char bitResult;
        char outflow = ‘0‘;
        string::iterator iterLarge = largeStr.end();
        string::iterator iterSmall = smallStr.end();
        string reversedResult;
        for(int tmp; lengthCommon != 0; lengthCommon--) {
            iterLarge--;
            iterSmall--;
            tmp = (*iterLarge - ‘0‘) + (*iterSmall - ‘0‘) + (outflow - ‘0‘);
            if(tmp == 0) {
                bitResult = ‘0‘;
                outflow = ‘0‘;
            }
            if(tmp == 1) {
                bitResult = ‘1‘;
                outflow = ‘0‘;
            }
            if(tmp == 2) {
                bitResult = ‘0‘;
                outflow = ‘1‘;
            }
            if(tmp == 3) {
                bitResult = ‘1‘;
                outflow = ‘1‘;
            }
            reversedResult.push_back(bitResult);
        }
        if(iterLarge == largeStr.begin()) {
            if(outflow == ‘1‘)
                reversedResult.push_back(‘1‘);
        }
        if(iterLarge != largeStr.begin()) {
            iterLarge--;
            int tmp;
            for(; iterLarge != largeStr.begin(); iterLarge--) {
                tmp = (*iterLarge - ‘0‘) + (outflow - ‘0‘);
                if(tmp == 0) {
                    bitResult = ‘0‘;
                    outflow = ‘0‘;
                }
                if(tmp == 1) {
                    bitResult = ‘1‘;
                    outflow = ‘0‘;
                }
                if(tmp == 2) {
                    bitResult = ‘0‘;
                    outflow = ‘1‘;
                }
                reversedResult.push_back(bitResult);
            }
            //the same as loop body above.because string.begin() - 1 is unavailable.
            tmp = (*iterLarge - ‘0‘) + (outflow - ‘0‘);
            if(tmp == 0) {
                bitResult = ‘0‘;
                outflow = ‘0‘;
            }
            if(tmp == 1) {
                bitResult = ‘1‘;
                outflow = ‘0‘;
            }
            if(tmp == 2) {
                bitResult = ‘0‘;
                outflow = ‘1‘;
            }
            reversedResult.push_back(bitResult);



            if(outflow == ‘1‘)
                reversedResult.push_back(‘1‘);
        }
        reverse(reversedResult.begin(), reversedResult.end());

        //cout << reversedResult << endl;
        return reversedResult;

    }
};
int main() {
    string a = "11";
    string b = "1";
    Solution solution;
    cout << solution.addBinary(a, b) << endl;
    getchar();

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Add Binary

标签:

原文地址:http://blog.csdn.net/guanzhongshan/article/details/46715817

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!