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_t len_b = b.length();
       if(len_a == 0)
        return b;
       if(len_b == 0)
        return a;
       int i = len_a - 1;
       int j = len_b - 1;
       int increase = 0;
       stack<int> result_stk;
       while(i >= 0 && j >= 0)
       {
           int s = a[i] - ‘0‘ + b[j] - ‘0‘ + increase;
           increase = s / 2;
           int sum = s % 2;
           result_stk.push(sum);
           i--;
           j--;
       }
       while(i >= 0)
       {
           int s = a[i] - ‘0‘ + increase;
           increase = s / 2;
           int sum = s % 2;
           result_stk.push(sum);
           i--;
       }
       while(j >= 0)
       {
           int s = b[j] - ‘0‘ + increase;
           increase = s / 2;
           int sum = s % 2;
           result_stk.push(sum);
           j--;
       }
       if(increase == 1)
       result_stk.push(1);

       string result;
       while(!result_stk.empty())
       {
           result += result_stk.top() + ‘0‘;
           result_stk.pop();
       }
       return result;
    }
};
时间: 2024-08-05 09:26:51

Leetcode:Add Binary 二进制相加的相关文章

[Leetcode] add binary 二进制加法

Given two binary strings, return their sum (also a binary string). For example,a ="11"b ="1"Return"100". 题意:将两个以字符串形式保存的二进制数进行相加. 思路:其实不管是以数组.字符串形式,加或者乘(multiply strings),一般的思路都是从后往前计算,用一个中间变量保存相加或者相乘的结果(加法是一个变量int 或string就行,

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

[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

[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 两个二进制数相加

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

[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 解题报告

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