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,它的长度是两条输入string中的较大的那个,并且把较短的那个输入string通过在开头加字符‘0’来补的较大的那个长度。这时候我们逐个从两个string的末尾开始取出字符,然后转为数字,想加,如果大于等于2,则标记进位标志carry,并且给新string加入一个字符‘0’。代码如下:

class Solution {
public:
    string addBinary(string a, string b) {
        string res;
        int na = a.size();
        int nb = b.size();
        int n = max(na, nb);
        bool carry = false;
        if (na > nb) {
            for (int i = 0; i < na - nb; ++i) b.insert(b.begin(), ‘0‘);
        }
        else if (na < nb) {
            for (int i = 0; i < nb - na; ++i) a.insert(a.begin(), ‘0‘);
        }
        for (int i = n - 1; i >= 0; --i) {
            int tmp = 0;
            if (carry) tmp = (a[i] - ‘0‘) + (b[i] - ‘0‘) + 1;
            else tmp = (a[i] - ‘0‘) + (b[i] - ‘0‘);
            if (tmp == 0) {
                res.insert(res.begin(), ‘0‘);
                carry = false;
            }
            else if (tmp == 1) {
                res.insert(res.begin(), ‘1‘);
                carry = false;
            }
            else if (tmp == 2) {
                res.insert(res.begin(), ‘0‘);
                carry = true;
            }
            else if (tmp == 3) {
                res.insert(res.begin(), ‘1‘);
                carry = true;
            }
        }
        if (carry) res.insert(res.begin(), ‘1‘);
        return res;
    }
};
时间: 2024-09-30 15:06:02

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] 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_

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;

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型然后相加,然后判断每一位给出一个新的字符串. 可是,越写越多,遇到各种不同的问题,比如

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 67. Add Binary 字符串

67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 思路: 1.将两个字符串按数组相加得到新数组. 2.将新数组转换成结果. 代码如下: class Solution { public:     string addBinary(string a, str