[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所对应的值以后就得到了1,0的数值

代码参考

public class Solution {
    /**
     * @param a a number
     * @param b a number
     * @return the result
     */
    public String addBinary(String a, String b) {
        // Write your code here

        int m=a.length();
        int n=b.length();
        int flag=0;
        String sum="";

        int maxLen=Math.max(m,n);
        for(int i=0;i<maxLen;i++)
        {
            int p=0,q=0;
            if(i<m)
            {
                p=a.charAt(m-1-i)-‘0‘;
            }
            else
            {
                p=0;
            }

            if(i<n)
            {
                q=b.charAt(n-1-i)-‘0‘;
            }
            else
            {
                q=0;
            }

            int temp=p+q+flag;
            flag=temp/2;
            sum=temp%2+sum;
        }
        if(flag==0)
        {
            return sum;
        }
        else
        {
            return "1"+sum;
        }

    }
}
时间: 2024-10-09 16:42:27

[lintcode easy]Add Binary的相关文章

[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

leetcode学习笔记:Add Binary

一.题目描述 Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 二.解题技巧 这道题考察两个二进制数相加,考虑到输入的是两组string,同时注意在运算时从左到右分别是从低位到高位,因此需要考虑对输入进行翻转处理,中间二进制树相加部分没有过多的设计障碍,主要是计算进位:在两组数

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

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". public class Solution { public String addBinary(String a, String b) { if(a.equalsIgnoreCase("")||a==null) r

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

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". 1 public class Solution { 2 public String addBinary(String a, String b) { 3 String res ="";

[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 *

Add Binary Leetcode java

题目: Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题解: 二进制加法都是从最低位(从右加到左).所以对两个字符串要从最后一位开始加,如果遇见长度不一的情况,就把短的字符串高位补0. 每轮计算要加上进位,最后跳出循环后要坚持进位是否为1,以便更新结果. 代码如下(from