[leetcode]Add Binary

原创 2015年11月21日 00:04:09

题目描述如下:

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

For example,
a = “11”
b = “1”
Return “100”.

接到题目轻松+愉悦地写了第一版:

public String addBinary(String a, String b) {
        int res = BinaryToInt(a) + BinaryToInt(b);
        return Integer.toBinaryString(res);
    }
    public int BinaryToInt(String num){
        int res = 0;
        for(int i = 0; i < num.length(); i++){
            res += (Math.pow(2, i)) * (num.charAt(num.length() - i - 1) - '0');
        }
        return res;
    }

然后当然因为天真挂掉了。输入的参考数据位数远超一般的类型,所以很显然这是对字符串的操作。之所以把这段放上来,因为toBinaryString这个函数好神奇啊以前都不知道没用过(确定不是你水么= =…)。

既然是字符串的加减法,常规的方法就是反转一下字符串,然后加个carry位表示进位,加的时候先处理较短的字符串。附上代码:

public class Solution {
   public String addBinary(String a, String b) {
        a = reverseStr(a);
        b = reverseStr(b);
        String longerStr = a.length() > b.length() ? a : b;
        String shortterStr = a.length() > b.length() ? b : a;
        String res = "";
        int i, tmp, carry = 0;
        for(i = 0; i < shortterStr.length(); i++){
            tmp = shortterStr.charAt(i) - '0' + longerStr.charAt(i) - '0' + carry;
            if(tmp >= 2){
                tmp -= 2;
                carry = 1;
            }else{
                carry = 0;
            }
            res += String.valueOf((char)(tmp + '0'));
        }
        for(; i < longerStr.length(); i++){
            tmp = longerStr.charAt(i) - '0' + carry;
            if(tmp >= 2){
                tmp -= 2;
                carry = 1;
            }else{
                carry = 0;
            }
            res += String.valueOf((char)(tmp + '0'));
        }
        if(carry == 1) res += "1";
        return reverseStr(res);
    }
    public String reverseStr(String s){
            StringBuffer sb=new StringBuffer(s);
            sb.reverse();
            return sb.toString();
        }
}

两个地方mark:

  1. java中反转字符串的方法(利用StringBuffer);
  2. String.valueOf((char)(tmp + ‘0’))这里,要进行强制转换。

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode 题目之4 Add Binary

题目: Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

Leetcode - Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = “11” b = “1”...

LeetCode题解:Add Binary

题目链接:Add Binary 题目描述: Given two binary strings, return their sum (also a binary string). For e...
  • yums467
  • yums467
  • 2015年09月08日 08:27
  • 268

LeetCode 067 Add Binary

题目 Given two binary strings, return their sum (also a binary string). For example,...
  • seawade
  • seawade
  • 2014年02月16日 09:22
  • 472

LeetCode笔记:67. Add Binary

模拟两个字符串二进制数的加法

Add Binary - LeetCode 67

题目描述:Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "...
  • bu_min
  • bu_min
  • 2015年04月24日 22:05
  • 268

Python学习——leetcode(Add Binary)

Add Binary: Given two binary strings, return their sum (also a binary string). For example, a = "11"...

Leetcode9: Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1...

LeetCode Add Binary

Description: Given two binary strings, return their sum (also a binary string). Solution: 将十进制的...

[Leetcode]Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode]Add Binary
举报原因:
原因补充:

(最多只允许输入30个字)