zoukankan      html  css  js  c++  java
  • 67. Add Binary

    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 aLen = a.length();
            int bLen = b.length();
            bool carry = false;
            int aPointer = aLen-1;
            int bPointer = bLen-1;
            if(aLen >= bLen){
                while(bPointer >=0)
                {
                    if(!carry){
                        if(a[aPointer] == '0' && b[bPointer] == '0')
                        {
                            a[aPointer] = '0';
                        }
                        else if(a[aPointer] == '1' && b[bPointer] == '1')
                        {
                            a[aPointer] = '0';
                            carry = true;
                        }
                        else
                        {
                            a[aPointer] = '1';
                        }
                    }
                    else
                    {
                        if(a[aPointer] == '0' && b[bPointer] == '0')
                        {
                            a[aPointer] = '1';
                            carry = false;
                        }
                        else if(a[aPointer] == '1' && b[bPointer] == '1')
                        {
                            a[aPointer] = '1';
                        }
                        else
                        {
                            a[aPointer] = '0';
                        }
                    }
                    aPointer--;
                    bPointer--;
                }
                while(aPointer>=0 && carry)
                {
                    if(a[aPointer] == '0')
                    {
                        a[aPointer] = '1';
                        carry = false;
                        break;
                    }
                    a[aPointer]='0';
                    aPointer--;
                }
                if(carry)
                {
                    a = "1" + a;
                }
                return a;
            }
            else{
                while(aPointer >=0)
                {
                    if(!carry){
                        if(a[aPointer] == '0' && b[bPointer] == '0')
                        {
                            b[bPointer] = '0';
                        }
                        else if(a[aPointer] == '1' && b[bPointer] == '1')
                        {
                            b[bPointer] = '0';
                            carry = true;
                        }
                        else
                        {
                            b[bPointer] = '1';
                        }
                    }
                    else
                    {
                        if(a[aPointer] == '0' && b[bPointer] == '0')
                        {
                            b[bPointer] = '1';
                            carry = false;
                        }
                        else if(a[aPointer] == '1' && b[bPointer] == '1')
                        {
                            b[bPointer] = '1';
                        }
                        else
                        {
                            b[bPointer] = '0';
                        }
                    }
                    aPointer--;
                    bPointer--;
                }
                while(bPointer>=0 && carry)
                {
                    if(b[bPointer] == '0')
                    {
                        b[bPointer] = '1';
                        carry = false;
                        break;
                    }
                    b[bPointer] = '0';
                    bPointer--;
                }
                if(carry)
                {
                    b = "1" + b;
                }
                return b;
            }
        }
    };
  • 相关阅读:
    【原创】R61509/SPFD5420A驱动调试总结
    [轉貼]程序员需要具备的基本技能
    Entity SQL 说说IN关键字
    细说可空类型 nullable
    C#值类型与引用类型之我见
    解密区域集成服务器
    序列化对象,使用的三种方式:Xml,Binary,Soap
    有时间就买点书
    hdu2680 Choose the best route
    SQLServer 函数类型简要说明
  • 原文地址:https://www.cnblogs.com/qionglouyuyu/p/4930161.html
Copyright © 2011-2022 走看看