[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 number
     * @return the result
     */
    string addBinary(string& a, string& b) {
        string res = "";
        int m = a.size() - 1, n = b.size() - 1, carry = 0;
        while (m >= 0 || n >= 0) {
            int p = m >= 0 ? a[m--] - ‘0‘ : 0;
            int q = n >= 0 ? b[n--] - ‘0‘ : 0;
            int sum = p + q + carry;
            res = to_string(sum % 2) + res;
            carry = sum / 2;
        }
        return carry == 1 ? "1" + res : res;
    }
};
时间: 2024-11-05 21:37:28

[LintCode] Add Binary 二进制数相加的相关文章

Add Binary 二进制数相加

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 二进制数想加,并且保存在string中,要注意的是如何将string和int之间互相转换,并且每位相加时,会有进位的可能,会影响之后相加的结果.而且两个输入string的长度也可能会不同.这时我们需要新建一个string,它的长度是两

[LeetCode] 67. Add Binary 二进制数相加

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路: 从最低位加到最高位,当前位相加结果是%2,进位是/2,记得处理每一次的进位和最后一次的进位,最后反向输出字符. Java: public class AddBinary { public String addBinary(St

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_

Lintcode: Add Binary

C++ 1 class Solution { 2 public: 3 /** 4 * @param a a number 5 * @param b a number 6 * @return the result 7 */ 8 string addBinary(string& a, string& b) { 9 // Write your code here 10 if (a == "0") { 11 return b; 12 } 13 if (b == "0&

LeetCode Add Binary 两个二进制数相加

1 class Solution { 2 public: 3 string addBinary(string a, string b) { 4 if(a==""&&b=="") return ""; 5 if(a=="") return b; 6 if(b=="") return a; 7 char *pa=&a[0],*pb=&b[0]; 8 int na=0,nb=0;

[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学习笔记:Add Binary

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

[LeetCode] Add Strings 字符串相加

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

Leet Code Add Binary

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 实现二进制数相加,算法很简单就是两个String每位去相加,然后判断是否要进位. 一开始想到了一个不同的算法,即将两个String转为int型然后相加,然后判断每一位给出一个新的字符串. 可是,越写越多,遇到各种不同的问题,比如