LeetCode--67. Add Binary

题目链接:https://leetcode.com/problems/add-binary/

这个就是要求将两个二进制表示的字符串相加返回一个二进制字符串,最需要注意的是进位的问题。

因为是easy的题目,所以思路比较多。

思路一:将两个二进制字符串转为对应的十进制整数,然后相加,将计算结果再转为二进制表达的字符串。这个思路比较粗暴,这里不做实现了。

思路二:最终结果字符串的长度要么等于a,b长度的较大者,要么比较大者多1,所以可以创建一个max(len(a),len(b))+1的数组,在”0“”1“相加时有四种情况直接枚举,注意进位的标记传递。

class Solution {
    public String addBinary(String a, String b) {
        int i=a.length()-1,j=b.length()-1;
        if(i<0)
            return b;
        if(j<0)
            return a;
        char[] ret=new char[Math.max(i,j)+2];
        int k=ret.length-1;
        boolean addition=false;
        while(i>=0 && j>=0)
        {
            if(a.charAt(i)=='1' && b.charAt(j)=='1')
            {
                if(addition)
                    ret[k]='1';
                else
                    ret[k]='0';
                addition=true;
            }
            else if((a.charAt(i)=='0' && b.charAt(j)=='1')||(a.charAt(i)=='1' && b.charAt(j)=='0'))
            {
                if(addition)
                {
                     ret[k]='0';
                    addition=true;
                }
                else
                    ret[k]='1';
            }
            else
            {
                if(addition)
                    ret[k]='1';
                else
                    ret[k]='0';
                addition=false;
            }
            i--;j--;k--;
        }
        
        if(i>=0)
        {
            while(i>=0)
            {
                if(a.charAt(i)=='0')
                {
                    if(addition)
                        ret[k]='1';
                    else
                        ret[k]='0';
                    addition=false;
                }
                else
                {
                    if(addition)
                    {
                        ret[k]='0';
                        addition=true;
                    }
                    else
                        ret[k]='1';
                }
                i--;k--;
            }
        }
        
        if(j>=0)
        {
            while(j>=0)
            {
                if(b.charAt(j)=='0')
                {
                    if(addition)
                        ret[k]='1';
                    else
                        ret[k]='0';
                    addition=false;
                }
                else
                {
                    if(addition)
                    {
                        ret[k]='0';
                        addition=true;
                    }
                    else
                        ret[k]='1';
                }
                j--;k--;
            }
        }
        if(addition)
             ret[0]='1';
        else
            ret[0]='0';
        String s=new String(ret);
        if(ret[0]=='1')
            return s.substring(0,s.length());
        else
            return s.substring(1,s.length());
    }
}

虽然写了100行但是执行效率还是满分的,时间复杂度为O(n),空间复杂度O(n).

猜你喜欢

转载自blog.csdn.net/To_be_to_thought/article/details/84895344