[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就行,乘就要用到向量了)。本题而言,要注意的是,若是相加时其中长度较小的字符串变量完时,怎么办?这时,其实可以用中间变量在字符串各自存在的前提下加上对应值就行。还有一个就是:字符和数字之间的相互转换,可以通过加减‘0‘来实现。最后一个问题是:最高位存在进位的情况,其实可以最后根据中间变量的值来进行处理。代码如下:

 1 class Solution {
 2 public:
 3     string addBinary(string a, string b)
 4     {
 5         string res="";
 6         int aLen=a.size()-1;
 7         int bLen=b.size()-1;
 8         int carry=0;
 9         while(aLen>=0||bLen>=0)
10         {
11             if(aLen>=0)
12                 carry+=a[aLen--]-‘0‘;
13             if(bLen>=0)
14                 carry+=b[bLen--]-‘0‘;
15
16             res=char(carry%2+‘0‘)+res;  //必须存在char不然,就会报错。恩,不懂。也可以是to_string(carry%2)+res;
17             carry/=2;
18         }
19         if(carry==1)
20         {
21             res=‘1‘+res;
22         }
23
24         return res;
25     }
26 };
时间: 2024-10-12 15:15:33

[Leetcode] 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". 思路:二进制加法,比较简单.代码如下: public class Solution { public String addBinary(String a, String b) { int len = Math.max(a.len

[leetcode]67. Add Binary 二进制加法

Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010&q

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

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 Add Binary 结题报告

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". 解题报告:二进制加法,分类为:Easy.没有任何算法,就是按照普通的方法计算即可. 一开始写了一个高低位reverse的,然后再从0到高位

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". 题意:二进制的加法. 思路:跟大数加法差点儿相同. class Solution { public: string addBinary(string a, string b) { stack<int> s; int