第30题 Add Binary

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

For example,

a = "11"

b = "1"

Return "100".

Hide Tags

Math String

Solution in Java:

public class Solution {
    public String addBinary(String a, String b) {
        int lengthA = a.length();
        int lengthB = b.length();
        int minLength, maxLength;
        String minStr, maxStr;
        if(lengthA<=lengthB){
            minLength = lengthA;
            minStr = a;
            maxLength = lengthB;
            maxStr = b;
        }else{
            minLength  = lengthB;
            minStr = b;
            maxLength = lengthA;
            maxStr = a;
        }
        String sum="";
        boolean carry = false;
        for(int i=0; i<minLength; i++){
            int digi1 = minStr.charAt(minLength-i-1)-'0';
            int digi2 = maxStr.charAt(maxLength-i-1)-'0';
            int sumDigi;
            if(carry)   sumDigi = digi1+digi2+1;
            else    sumDigi = digi1+digi2;
            if(sumDigi/2>0) carry = true;
            else    carry = false;
            sumDigi = sumDigi%2;
            String curDigi = String.valueOf(sumDigi);
            sum = curDigi +sum;
        }
        for(int i=maxLength-minLength-1; i>=0; i--){
            int digi = maxStr.charAt(i)-'0';
            if(carry) digi = digi+1;
            if(digi==2){
                digi=0;
                carry = true;
            }
            else    carry = false;
            String curDigi = String.valueOf(digi);
            sum = curDigi+sum;
        }
        if(carry)   sum = "1"+sum;
        return  sum;
    }
}

Note:

本来想直接用java中二进制,十进制和String间的转换,函数是:

二进制转十进制:int i=Integer.parseInt(str, 2)
第二个参数为进制树

十进制数转二进制String:String binStr = Integer.toBinaryString(i)

但在此题中不行,因为两个输入参数可能超过int和long的表示范围,会发生NumberFormatException,只能直接在String上操作。

获取String某一位的字符为str.charAt(i),得到char的int值,可以用这个char减去‘0’,可以得到该位的0或1的值。char可以直接做减法。

int转为String用函数String str= String.valueOf(i)

注意这个加法操作要从String的末尾到开头进行

时间: 2024-10-08 02:28:31

第30题 Add Binary的相关文章

LeetCode 第 67 题 (Add Binary)

LeetCode 第 67 题 (Add Binary) Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 两个字符串,计算加法.这道题主要是考察对字符串操作的掌握情况.另外,加法要从低位算起,但是输出时要先输出高位.因此,需要将计算结果先存下来,然后再逆序输出. 访问字符串的

leetcode_67题——Add Binary(字符串string,反向迭代器reverse_iterator,栈stack)

Add Binary Total Accepted: 39288 Total Submissions: 158078My Submissions Question Solution Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". Hide Tags Math String Have

【leetcode刷题笔记】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 题解:简单的二进制加法模拟.a,b的最后以为对齐开始进行加法,用carries保存进位,如果加完后最高位还有进位,那么要在结果的最前面加一个1. 代码如下: 1 public class Solution { 2 public Str

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 @ 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

[lintcode easy]Add Binary

Add Binary Given two binary strings, return their sum (also a binary string).   Example a = 11 b = 1 Return 100 solution: 比较两个string的长度,将长读较小的string左边用0补齐. 设置进位标志flag.循环结束后如果进位标识大于0,则返回进位加上其他string:否则返回新string: char 0对应ASCII码表中30 1对应31....减去0所对应的值以后就

[LeetCode][JavaScript]Add Binary

https://leetcode.com/problems/add-binary/ Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 长整型二进制加法. 1 /** 2 * @param {string} a 3 * @param {string} b 4 *

[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

[LintCode] Add Binary 二进制数相加

Given two binary strings, return their sum (also a binary string). Have you met this question in a real interview? Yes Example a = 11 b = 1 Return 100 LeetCode上的原题,请参见我之前的博客Add Binary. class Solution { public: /** * @param a a number * @param b a num