zoukankan      html  css  js  c++  java
  • (String) leetcode 67. Add Binary

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

    The input strings are both non-empty and contains only characters 1or 0.

    Example 1:

    Input: a = "11", b = "1"
    Output: "100"

    Example 2:

    Input: a = "1010", b = "1011"
    Output: "10101"

    ---------------------------------------------------------------------------------------------------------------------------------------------

    这个题说的是将两个以字符串形式的二进制进行二进制加法运算后得到的结果,结果是string形式。

    1)

    先从最右端遍历,一个一个相加,可以用一个辅助变量来作为要进位的数,如果其中一个已经遍历完,而另一个还没遍历完,再继续遍历这个没有遍历完的字符串(并不是与另一个字符串二进制对应的数相加)。

    用stringstream来将一个int类型的数转换为string类型,不过花的时间可能会比较长。

    C++代码:

    class Solution {
    public:
        string addBinary(string a, string b) {
            int len1 = a.length();
            int len2 = b.length();
            int i = len1-1,j = len2-1;
            int carry = 0;  //进位。
            string str = "";
            while(true){
                if(i < 0 || j < 0){
                    break;
                }
                int num1 = a[i] - '0';
                int num2 = b[j] - '0';
                int sum = num1 + num2 + carry;
                stringstream ss;
                string s;
                ss << (sum%2);
                ss >> s;
                str = s + str;
                carry = sum / 2;  //向前进1或进0。
                i--;
                j--;
            }
            if(j >= 0){
                while(j >= 0){
                    int num = b[j] - '0';
                    int sum = num + carry;
                    stringstream ss;
                    string s;
                    ss << (sum%2);
                    ss >> s;
                    str = s + str;
                    carry = sum / 2;
                    j--;
                }
            }
            if(i >= 0){
                while(i >= 0){
                    int num = a[i] - '0';
                    int sum = num + carry;
                    stringstream ss;
                    string s;
                    ss << (sum%2);
                    ss >> s;
                    str = s + str;
                    carry = sum / 2;
                    i--; 
                }
            }
            if(carry > 0){
                stringstream ss;
                string s;
                ss << carry;
                ss >> s;
                str = s + str;
            }
            return str;
        }
    };

    2)

    也可以直接用char(num + '0'),将一个int类型的数转换为char类型。这样会大大提升速度。

    C++代码:

    class Solution {
    public:
        string addBinary(string a, string b) {
            int len1 = a.length();
            int len2 = b.length();
            int i = len1-1,j = len2-1;
            int carry = 0;
            string str = "";
            while(true){
                if(i < 0 || j < 0){
                    break;
                }
                int num1 = a[i] - '0';
                int num2 = b[j] - '0';
                int sum = num1 + num2 + carry;
                str = char(sum%2 + '0') + str;
                carry = sum / 2;
                i--;
                j--;
            }
            if(j >= 0){
                while(j >= 0){
                    int num = b[j] - '0';
                    int sum = num + carry;
                    str = char(sum%2 + '0') + str;
                    carry = sum / 2;
                    j--;
                }
            }
            if(i >= 0){
                while(i >= 0){
                    int num = a[i] - '0';
                    int sum = num + carry;
                    str = char(sum % 2 + '0') + str;
                    carry = sum / 2;
                    i--; 
                }
            }
            if(carry > 0){
                str = char(carry + '0') + str;
            }
            return str;
        }
    };

    3)

    还可以吧上面的代码进一步优化,缩短代码行。要熟练地运用三元表达式。

    C++代码:

    class Solution {
    public:
        string addBinary(string a, string b) {
            int len1 = a.length();
            int len2 = b.length();
            int carry = 0;
            string str = "";
            for(int i = len1 - 1,j = len2 - 1;i >= 0 || j >= 0;i--,j--){
                int num1 = i >= 0 ? a[i] - '0' : 0;
                int num2 = j >= 0 ? b[j] - '0' : 0;
                int sum = num1 + num2 + carry;
                str = char(sum % 2 + '0') + str;
                carry = sum / 2;
            }
            if(carry > 0){
                str = char(carry + '0') + str;
            }
            return str;
        }
    };

    4)

    这个char还可以用to_string()代替,用to_string(num),这个是<string> 内置的。不过运行速度会变慢。。。。

    C++代码:

    class Solution {
    public:
        string addBinary(string a, string b) {
            int len1 = a.length();
            int len2 = b.length();
            int carry = 0;
            string str = "";
            for(int i = len1 - 1,j = len2 - 1;i >= 0 || j >= 0;i--,j--){
                int num1 = i >= 0 ? a[i] - '0' : 0;
                int num2 = j >= 0 ? b[j] - '0' : 0;
                int sum = num1 + num2 + carry;
                str = to_string(sum % 2) + str;
                carry = sum / 2;
            }
            if(carry > 0){
                str = to_string(carry) + str;
            }
            return str;
        }
    };
  • 相关阅读:
    SAP HUM事务代码 HUMAT 之初探
    SAP HUM 锁住一个HU?
    SAP MM 标准采购组织的分配对于寄售采购订单收货的影响
    SAP MM已经转成PO的采购申请Item依旧可以被删除?
    SAP MM A工厂下的PR可以转成B工厂下的PO?
    SAP MM 明明已经扩展供应商到采购组织下,采购订单里还是报错?
    SAP HUM 事务代码HUMO为整托做Scrap
    2018-8-29-Roslyn-静态分析
    2018-2-13-win10-UWP-等级控件
    2018-2-13-win10-uwp-改变鼠标
  • 原文地址:https://www.cnblogs.com/Weixu-Liu/p/10781024.html
Copyright © 2011-2022 走看看