67. Add Binary

本文介绍了一种解决两个二进制字符串相加问题的方法。通过遍历字符串并使用简单的位运算来实现相加,同时处理进位情况。提供两种实现方案:一种使用字符比较,另一种则利用字符的ASCII值进行计算。

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

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

简单方法,依次相加,若一个string加完还有进一,则对接下来的继续加

class Solution {
public:
    string addBinary(string a, string b) {
        int lena=a.length();
        int lenb=b.length();
        if(lena==0){
            return b;
        }
        if(lenb==0){
            return a;
        }
        int count=0;
        if(lena<lenb){
            string tmp=a;
            a=b;
            b=tmp;
            int tmp1=lena;
            lena=lenb;
            lenb=tmp1;
        }
        
            for(int i=lenb-1,j=lena-1;i>=0;i--,j--){
                if(count==0){
                    if((a[j]=='1')&&(b[i]=='1')){
                        a[j]='0';
                        count=1;
                    }
                    else if((a[j]=='1')||(b[i]=='1')){
                        a[j]='1';
                        count=0;
                    }
                }
                else{
                    
                    if((a[j]=='1')&&(b[i]=='1')){
                        a[j]='1';
                        count=1;
                    }
                    else if((a[j]=='1')||(b[i]=='1')){
                        a[j]='0';
                        count=1;
                    }
                    else{
                        a[j]='1';
                        count=0;
                    }
                }
            }
            for(int i=lena-lenb-1;i>=0;i--){
                if(count==1){
                    if(a[i]=='1'){
                        a[i]='0';
                    }
                    else{
                        a[i]='1';
                        count =0;
                    }
                }
                else{
                    break;
                }
            }
            if(count==1){
                a='1'+a;
            }
        
        return a;
    }
};

改进后,char型可直接进行加减,即不需要判断,虽改进不大,确是不同的方式

class Solution {
public:
    string addBinary(string a, string b) {
        int lena=a.length();
        int lenb=b.length();
        if(lena==0){
            return b;
        }
        if(lenb==0){
            return a;
        }
        int count=0;
        if(lena<lenb){
            string tmp=a;
            a=b;
            b=tmp;
            int tmp1=lena;
            lena=lenb;
            lenb=tmp1;
        }
        int sum=0;
            for(int i=lenb-1,j=lena-1;i>=0;i--,j--){
                
                sum=a[j]-'0'+b[i]-'0'+count;
                if(sum==1){
                    a[j]='1';
                    count=0;;
                }
                else if(sum==2){
                    a[j]='0';
                    count=1;
                }else if(sum==3){
                    a[j]='1';
                    count=1;
                }

            }
            for(int j=lena-lenb-1;j>=0;j--){
                sum=a[j]-'0'+count;
                if(sum==0){
                    count=0;
                    break;
                }
                else if(sum==1){
                    a[j]='1';
                    count=0;
                    break;
                }
                else if(sum==2){
                    a[j]='0';
                    count=1;
                }
            }
            if(count==1){
                a='1'+a;
            }
        
        return a;
    }
};




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值