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 = ; i < na - nb; ++i) b.insert(b.begin(), '');
}
else if (na < nb) {
for (int i = ; i < nb - na; ++i) a.insert(a.begin(), '');
}
for (int i = n - ; i >= ; --i) {
int tmp = ;
if (carry) tmp = (a[i] - '') + (b[i] - '') + ;
else tmp = (a[i] - '') + (b[i] - '');
if (tmp == ) {
res.insert(res.begin(), '');
carry = false;
}
else if (tmp == ) {
res.insert(res.begin(), '');
carry = false;
}
else if (tmp == ) {
res.insert(res.begin(), '');
carry = true;
}
else if (tmp == ) {
res.insert(res.begin(), '');
carry = true;
}
}
if (carry) res.insert(res.begin(), '');
return res;
}
};

下面这种写法又巧妙又简洁,用了两个指针分别指向a和b的末尾,然后每次取出一个字符,转为数字,若无法取出字符则按0处理,然后定义进位carry,初始化为0,将三者加起来,对2取余即为当前位的数字,对2取商即为当前进位的值,记得最后还要判断下carry,如果为1的话,要在结果最前面加上一个1,参见代码如下:

解法二:

class Solution {
public:
string addBinary(string a, string b) {
string res = "";
int m = a.size() - , n = b.size() - , carry = ;
while (m >= || n >= ) {
int p = m >= ? a[m--] - '' : ;
int q = n >= ? b[n--] - '' : ;
int sum = p + q + carry;
res = to_string(sum % ) + res;
carry = sum / ;
}
return carry == ? "" + res : res;
}
};

参考资料:

https://discuss.leetcode.com/topic/8981/short-code-by-c

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Add Binary 二进制数相加的更多相关文章

  1. [LintCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...

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

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  3. LeetCode Add Binary 两个二进制数相加

    class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...

  4. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  5. LeetCode: Add Binary 解题报告

    Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...

  6. [Leetcode] add binary 二进制加法

    Given two binary strings, return their sum (also a binary string). For example,a ="11"b =& ...

  7. LeetCode——Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  8. [leetcode]Add Binary @ Python

    原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (al ...

  9. Leetcode Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

随机推荐

  1. C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍

    在前面一系列文章中,我们可以看到微信自定义菜单的重要性,可以说微信公众号账号中,菜单是用户的第一印象,我们要规划好这些菜单的内容,布局等信息.根据微信菜单的定义,我们可以看到,一般菜单主要分为两种,一 ...

  2. Fragment

    Fragment 是轻量级的,比Activity 快,适合同一个APP 内页面的跳转. 1: 在MainActivity 中启动一个fragment  BlankFragment: public cl ...

  3. 【Java每日一题】20161229

    package Dec2016; import java.util.ArrayList; import java.util.List; public class Ques1229 { public s ...

  4. JAVA基础培训(isoft)

    我们

  5. 【笔记】LAMP 环境无脑安装配置 Centos 6.3

    p.p1 { margin: 0.0px 0.0px 5.0px 0.0px; font: 12.0px Times; color: #ff2500 } p.p2 { margin: 0.0px 0. ...

  6. CSS3和javascript中的transform

    在javascript中,WebkitTransform 大概相当于 transform .transform 为标准,WebkitTransform 适用于Webkit浏览器.js中的WebkitT ...

  7. Atitit ftp原理与解决方案

    Atitit ftp原理与解决方案 Deodeo sh shmayama ..search ftp.. 1. http和ftp都只是通信协议,就是只管传输那一块的,那为什么不能使用ftp来显示网页?? ...

  8. 小程序https Android 安卓可以发request请求,IOS 苹果 发请求失败问题

    如果一个机器可以发送成功,一个机器发送失败,那多半是是域名的https支持的问题 那就用腾讯云的这个ssl测试工具检测下 https://www.qcloud.com/product/ssl#user ...

  9. Android LocalBroadcastManager 的使用总结

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6048369.html 本文出自[赵彦军的博客] 前言 在Android中,Broadcast是一种广泛运用的 ...

  10. React Native学习笔记之2

    1:如何创建一个react native工程 首先进入到指定文件夹里面,然后在终端执行react-native init ReactNativeProject :其中ReactNativeProjec ...