leetcode 67. 二进制求和

这道题目和十进制题目很类似,但是在做的时候发现对string的用法不熟悉,首先是reverse,这个是对vector和string都起作用的std 函数,可以将对象反序,其次是在string中增加元素,使用的是push_back(),在写的时候对string取元素的方法也是印象模糊,后面尽量写一篇对string用法的文章增加记忆;

刚开始是想转换成int类型后计算,但是发现很繁琐,还是用字符吧,给出了一个笨办法:

class Solution {
public:
    string addBinary(string a, string b) {
        if (a.length() == 0) {
            return b;
        }
        if (b.length() == 0) {
            return b;
        }
        reverse(a.begin(),a.end());
        reverse(b.begin(),b.end());

        char carry = '0';

        int a_index = 0;
        int b_index = 0;
        string result;
        while (a_index < a.length() || b_index < b.length()) {
            if (a_index < a.length() && b_index < b.length()) {
                if (a[a_index] == '1' && b[b_index] == '1') {
                    if (carry == '1') {
                        result.push_back('1');
                    }
                    else {
                        result.push_back('0');
                        carry = '1';
                    }
                }
                else if (a[a_index] == '0' && b[b_index] == '0') {
                    if (carry == '1') {
                        result.push_back('1');
                        carry = '0';
                    }
                    else {
                        result.push_back('0');
                    } 
                }
                else {
                    if (carry == '1') {
                        result.push_back('0');
                    }
                    else {
                        result.push_back('1');
                    }
                    
                }
            }
            else {
                if (a_index == a.length()) {
                    if (carry == '1') {
                        if (b[b_index] == '1') {
                            result.push_back('0');
                        }
                        else {
                            result.push_back('1');
                            carry = '0';
                        }
                    }
                    else {
                        result.push_back(b[b_index]);
                    }
                    
                }
                else if (b_index == b.length()){
                    if (carry == '1') {
                        if (a[a_index] == '1') {
                            result.push_back('0');
                        }
                        else {
                            result.push_back('1');
                            carry = '0';
                        }
                    }
                    else {
                        result.push_back(a[a_index]);
                    }
                }
            }

            if (a_index < a.length()) {
                a_index++;
            }
            if (b_index < b.length()) {
                b_index++;
            }
            
        }

        if (carry == '1') {
            result.push_back('1');
        }

        reverse(result.begin(), result.end());
        return result;
       
    }
};

 后续我再增加新的解法

原文地址:https://www.cnblogs.com/rulin/p/13185601.html