LeetCode Add Binary |My Solution

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

For example,

a = "11"

b = "1"

Return "100".

Show Tags

Have you been asked this question in an interview?

public class Solution {
    public String addBinary(String a, String b) {
        if (a == null || a.length() == 0) {
            return b;
        }
        if (b == null || b.length() == 0) {
            return a;
        }
        int addBits = Math.min(a.length(), b.length());
        StringBuffer sb = new StringBuffer();
        int bitPlus = 0;
        for (int i = 0; i < addBits; i++) {
          int aBit = a.charAt(a.length() - 1 - i) - '0';
          int bBit = b.charAt(b.length() - 1 - i) - '0';
          int cur = aBit + bBit + bitPlus;
          bitPlus = cur / 2;
          cur = cur % 2;
          sb.insert(0, String.valueOf(cur));
        }
      return  doOther( a,  b, addBits, bitPlus, sb); 

    }
    public String doOther(String a, String b, int addBits,int bitPlus,StringBuffer sb) {
        if (b.length() > a.length()) {
            doOther( b, a, addBits, bitPlus, sb);
        } else {
            for (int i = addBits; i < a.length();i++)
            {
             int aBit = a.charAt(a.length() - 1 - i ) - '0';
             int cur = aBit + bitPlus;
             bitPlus = cur / 2;
             cur = cur % 2;
             sb.insert(0,String.valueOf(cur));
            }
            if (bitPlus == 1) {
                sb.insert(0,"1");
            }
            return sb.toString();
        }
        return sb.toString();

    }
}
时间: 2024-10-24 22:00:35

LeetCode Add Binary |My Solution的相关文章

[leetcode]Add Binary @ Python

原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 解题思路:提供两种实现方式吧. 代码一: class Solution: # @param a, a string # @pa

[LeetCode] [Add Binary 2012-04-02 ]

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". string 的操作,短string补位.两个"0"会输出一个"00",要特殊处理,plus如果最后为"1",要补上. ? 1 2 3 4 5 6 7 8 9 10 1

LeetCode——Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 求数字字符串的二进制和.同之前的数组代表数字,两个数组相加一样,只不过进位变成了2.可能两个串的长度不一样,故逆转,从左到右加下去,最后再逆转. public static String addBinary(String a,

LeetCode: Add Binary 解题报告

Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". SOLUTION: 指针指到两个字符串的末尾,不断往前推进,用carry表示进位.用stringbuilder来记录结果. 使用insert(0, c)函数将加出的结果不断插入到STRINGBUILDER. 1 p

LeetCode Add Binary 结题报告

https://oj.leetcode.com/problems/add-binary/ Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 解题报告:二进制加法,分类为:Easy.没有任何算法,就是按照普通的方法计算即可. 一开始写了一个高低位reverse的,然后再从0到高位

LeetCode Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题意:二进制的加法. 思路:跟大数加法差点儿相同. class Solution { public: string addBinary(string a, string b) { stack<int> s; int

[Leetcode] add binary 二进制加法

Given two binary strings, return their sum (also a binary string). For example,a ="11"b ="1"Return"100". 题意:将两个以字符串形式保存的二进制数进行相加. 思路:其实不管是以数组.字符串形式,加或者乘(multiply strings),一般的思路都是从后往前计算,用一个中间变量保存相加或者相乘的结果(加法是一个变量int 或string就行,

leetcode Add Binary (easy) /java

二进制的加法 经过测试之后,发现没有考虑整型数据溢出. leetcode经常会有一些意想不到的例外.我以为是一道解法很丰富的题,选择了:把string转为int,用int的加法,再转为string返回.因为我讨厌用字符串来进位.但是溢出了.可以改进一下,用BigInteger这个类. 先贴上我的错误代码. import java.io.*; import java.util.*; import java.lang.*; public class Solution { public static

Leetcode:Add Binary 二进制相加

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 思路同十进制的大数相加.代码如下: class Solution { public: string addBinary(string a, string b) { size_t len_a = a.length(); size_