67. Add Binary(easy)

版权声明:文章都是原创,转载请注明~~~~ https://blog.csdn.net/SourDumplings/article/details/86661642

Easy

784163FavoriteShare

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

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

Example 1:

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

Output: "100"

Example 2:

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

Output: "10101"

C++:

/*
 @Date    : 2019-01-26 21:53:59
 @Author  : 酸饺子 ([email protected])
 @Link    : https://github.com/SourDumplings
 @Version : $Id$
*/

/*
https://leetcode.com/problems/add-binary/
 */

class Solution
{
public:
    string addBinary(string a, string b)
    {
        string res;
        int pa = a.length() - 1, pb = b.length() - 1;
        int lastAdd = 0;
        char thisD;
        while (pa >= 0 && pb >= 0)
        {
            if (a[pa] - '0' + b[pb] - '0' + lastAdd >= 2)
            {
                thisD = a[pa] - '0' + b[pb] - '0' + lastAdd - 2 + '0';
                lastAdd = 1;
            }
            else
            {
                thisD = a[pa] - '0' + b[pb] - '0' + lastAdd + '0';
                lastAdd = 0;
            }
            res += thisD;
            --pa;
            --pb;
        }
        while (pa >= 0)
        {
            if (a[pa] - '0' + lastAdd >= 2)
            {
                thisD = a[pa] - '0' + lastAdd - 2 + '0';
                lastAdd = 1;
            }
            else
            {
                thisD = a[pa] - '0' + lastAdd + '0';
                lastAdd = 0;
            }
            res += thisD;
            --pa;
        }
        while (pb >= 0)
        {
            if (b[pb] - '0' + lastAdd >= 2)
            {
                thisD = b[pb] - '0' + lastAdd - 2 + '0';
                lastAdd = 1;
            }
            else
            {
                thisD = b[pb] - '0' + lastAdd + '0';
                lastAdd = 0;
            }
            res += thisD;
            --pb;
        }
        if (lastAdd == 1)
        {
            res += '1';
        }
        reverse(res.begin(), res.end());
        return std::move(res);
    }
};

Java:

/**
 * @Date    : 2019-01-26 22:46:17
 * @Author  : 酸饺子 ([email protected])
 * @Link    : https://github.com/SourDumplings
 * @Version : $Id$
 *
 * https://leetcode.com/problems/add-binary/
*/

import java.util.Vector;

class Solution
{
    public String addBinary(String a, String b)
    {
        Vector res = new Vector();
        int pa = a.length() - 1, pb = b.length() - 1;
        int lastAdd = 0;
        char thisD;
        while (pa >= 0 && pb >= 0)
        {
            if ((int)(a.charAt(pa) - '0') + (int)(b.charAt(pb) - '0') + lastAdd>= 2)
            {
                thisD = (char)((int)(a.charAt(pa) - '0') + (int)(b.charAt(pb) - '0') +
                    lastAdd - 2 + 48);
                lastAdd = 1;
            }
            else
            {
                thisD = (char)((int)(a.charAt(pa) - '0') + (int)(b.charAt(pb) - '0') +
                    lastAdd + 48);
                lastAdd = 0;
            }
            res.add(thisD);
            --pa;
            --pb;
        }
        while (pa >= 0)
        {
            if ((int)(a.charAt(pa) - '0') + lastAdd >= 2)
            {
                thisD = (char)((int)(a.charAt(pa) - '0') + lastAdd - 2 + 48);
                lastAdd = 1;
            }
            else
            {
                thisD = (char)((int)(a.charAt(pa) - '0') + lastAdd + 48);
                lastAdd = 0;
            }
            res.add(thisD);
            --pa;
        }
        while (pb >= 0)
        {
            if ((int)(b.charAt(pb) - '0') + lastAdd >= 2)
            {
                thisD = (char)((int)(b.charAt(pb) - '0') + lastAdd - 2 + 48);
                lastAdd = 1;
            }
            else
            {
                thisD = (char)((int)(b.charAt(pb) - '0') + lastAdd + 48);
                lastAdd = 0;
            }
            res.add(thisD);
            --pb;
        }
        if (lastAdd == 1)
        {
            res.add('1');
        }
        Collections.reverse(res);
        int n = res.size();
        StringBuffer ret = new StringBuffer(n);
        for (int i = 0; i != n; ++i)
        {
            ret.append(String.valueOf(res.get(i)));
        }
        return ret.toString();
    }
}


猜你喜欢

转载自blog.csdn.net/SourDumplings/article/details/86661642
今日推荐