public class Solution {
    public string AddBinary(string a, string b) {
        var list = new List<string>();

            if (a.Length > b.Length)
            {
                //补充b的长度
                var dif = a.Length - b.Length;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < dif; i++)
                {
                    sb.Append("0");
                }
                b = sb.ToString() + b;

                var len = a.Length;
                var jin = 0;
                for (int i = len - 1; i >= 0; i--)
                {
                    var ca = Convert.ToInt32(a[i].ToString());
                    var cb = Convert.ToInt32(b[i].ToString());
                    var cur = ca + cb + jin;
                    if (cur >= 2)
                    {
                        jin = 1;
                    }
                    else
                    {
                        jin = 0;
                    }
                    cur = cur % 2;
                    list.Add(cur.ToString());
                }
                if (jin == 1)
                {
                    list.Add("1");
                }
            }
            else
            {
                var dif = b.Length - a.Length;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < dif; i++)
                {
                    sb.Append("0");
                }
                a = sb.ToString() + a;

                var len = b.Length;
                var jin = 0;
                for (int i = len - 1; i >= 0; i--)
                {
                    var ca = Convert.ToInt32(a[i].ToString());
                    var cb = Convert.ToInt32(b[i].ToString());
                    var cur = ca + cb + jin;
                    if (cur >= 2)
                    {
                        jin = 1;
                    }
                    else
                    {
                        jin = 0;
                    }
                    cur = cur % 2;
                    list.Add(cur.ToString());
                }
                if (jin == 1)
                {
                    list.Add("1");
                }
            }

            StringBuilder sbx = new StringBuilder();
            for (int i = list.Count - 1; i >= 0; i--)
            {
                sbx.Append(list[i]);
            }
            return sbx.ToString();
    }
}

https://leetcode.com/problems/add-binary/#/description

 

Java的实现:

 1 class Solution {
 2     public String addBinary(String a, String b) {
 3         StringBuilder ans = new StringBuilder();
 4 
 5         int n = Math.max(a.length(), b.length()), carry = 0;
 6         for (int i = 0; i < n; ++i) {
 7             carry += i < a.length() ? (a.charAt(a.length() - 1 - i) - '0') : 0;
 8             carry += i < b.length() ? (b.charAt(b.length() - 1 - i) - '0') : 0;
 9             ans.append((char) (carry % 2 + '0'));
10             carry /= 2;
11         }
12 
13         if (carry > 0) {
14             ans.append('1');
15         }
16         ans.reverse();
17 
18         return ans.toString();
19     }
20 }

 

posted on 2017-04-25 12:02  Sempron2800+  阅读(124)  评论(0编辑  收藏  举报