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 1 or 0.

Example 1:

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

Example 2:

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

解題思路:

第一種作法:
將2位元字串皆轉成10位元數字,然後進行相加後將相加結果再轉乘2位元,已知的問題為2位元轉為10位元有可能發生int溢位問題

C# int 有效範圍 -2147483648 ~ 2147483647
C# int16 有效範圍 -32768 ~ 32767
C# int32 有效範圍 -2147483648 ~ 2147483647
C# int64 有效範圍 -9223372036854775808 ~ 9223372036854775807

第二種作法:

為了防止int溢位問題,我們可以先將兩個字串轉為字元陣列,並使用for迴圈針對每個元素進行相加處理,且設定一個carry用來放置進位資料。

再進行字元轉換成數字時要注意必須先將字元減去'0'字元(Ascii code: 48)的位置再進行ToInt的轉換才能取得正確數字。

using System;
using System.Collections.Generic;

namespace Add_Binary
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = "10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101";
            string b = "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011";
            string result = AddBinary(a, b);
            Console.WriteLine(result);
        }

        public static string AddBinary(string a, string b)
        {
            char[] aCharArr = a.ToCharArray();
            char[] bCharArr = b.ToCharArray();

            //陣列反向後使用迴圈從前面両兩相加,迴圈使用陣列比較大的那個

            char[] maxCharArr, minCharArr;

            if (aCharArr.Length >= bCharArr.Length)
            {
                maxCharArr = aCharArr;
                minCharArr = bCharArr;
            }
            else
            {
                maxCharArr = bCharArr;
                minCharArr = aCharArr;
            }

            Array.Reverse(maxCharArr);
            Array.Reverse(minCharArr);

            int carry = 0, sum = 0;
            List<string> result = new List<string>();

            for (int i = 0; i <= maxCharArr.Length - 1; i++)
            {
                if (i < minCharArr.Length)
                    sum = Convert.ToInt32(maxCharArr[i] - '0') + Convert.ToInt32(minCharArr[i] - '0') + carry;
                else
                    sum = Convert.ToInt32(maxCharArr[i] - '0') + carry;

                if (sum == 0)
                {
                    carry = 0;
                    result.Add("0");
                }
                else if (sum == 1)
                {
                    carry = 0;
                    result.Add("1");
                }
                else if (sum == 2)
                {
                    carry = 1;
                    result.Add("0");
                }
                else if (sum == 3)
                {
                    carry = 1;
                    result.Add("1");
                }
            }

            if (carry != 0)
                result.Add("1");

            result.Reverse();

            return string.Join("", result);
        }

        //public static string AddBinary(string a, string b)
        //{
        //    long aNum = Convert.ToInt64(a, 2);
        //    long bNum = Convert.ToInt64(b, 2);
        //    long sum = aNum + bNum;
        //    string binary = Convert.ToString(sum, 2);
        //    return binary;
        //}
    }
}