LeetCode 67. Add Binary
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
这个题目只要注意各种情况你就成功了一大半,特别要注意的是对进位赋值后可能产生的变化,以及最后一位进位为1时,
我们要把这个1插进来。同时注意字符串的低位是我们数值的高位
class Solution {
public:
string addBinary(string a, string b) {
reverse(begin(a), end(a));
reverse(begin(b), end(b));
string result;
char num = '';
int i = ;
for (;i < a.size() && i < b.size();++i)
{
if (a[i] == ''&&b[i] == ''&&num == '')
{
result.insert(begin(result), '');
}
if (a[i] == ''&&b[i] == ''&&num == '')
{
result.insert(begin(result), '');
}
if ((a[i] == ''&&b[i] == ''&&num == '') || (a[i] == ''&&b[i] == ''&&num == '') ||
(a[i] == ''&&b[i] == ''&&num == ''))
{
result.insert(begin(result), '');
num = '';
}
if (a[i] == ''&&b[i] == ''&&num == '' || a[i] == ''&&b[i] == ''&&num == '' ||
a[i] == ''&&b[i] == ''&&num == '')
{
result.insert(begin(result), '');
num = '';
}
}
if (i == a.size())
{
for (;i < b.size();++i)
{
if (b[i] == ''&&num == '')
result.insert(begin(result), '');
if (b[i] == ''&&num == '' || b[i] == ''&&num == '')
{
result.insert(begin(result), '');
num = '';
}
if (b[i] == ''&&num == '')
result.insert(begin(result), '');
}
}
else
{
for (;i < a.size();++i)
{
if (a[i] == ''&&num == '')
result.insert(begin(result), '');
if (a[i] == ''&&num == '')
result.insert(begin(result), '');
if (a[i] == ''&&num == '' || a[i] == ''&&num == '')
{
result.insert(begin(result), '');
num = '';
} }
}
if (num == '')result.insert(begin(result), '');
return result;
}
};
LeetCode 67. Add Binary的更多相关文章
- LeetCode 67. Add Binary (二进制相加)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- Java [Leetcode 67]Add Binary
题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...
- (String) leetcode 67. Add Binary
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- [leetcode]67. Add Binary 二进制加法
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- LeetCode - 67. Add Binary(4ms)
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- leetcode 67. Add Binary (高精度加法)
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- LeetCode 67 Add Binary(二进制相加)(*)
翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
随机推荐
- 利用Swift之协议语法实现页面间的传值功能
随着Swift 新开发语言的发布,又随着Xcode6.0.1的正式发布,利用swift编写iOS代码迫在眉睫,笔者在使用Objective-C开发近三年以来,对这种优雅的语法深感赞叹,下面我将对比式的 ...
- PCA的数学原理
PCA(Principal Component Analysis)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量,常用于高维 数据的 ...
- samba配置只读共享
编辑smb.conf 1.在[global]中 找到 security = 将其改为 security = share 2. 在文件中加入自定义的共享目录 [attachment] path=/dat ...
- mongodb不同版本之间有很大的差异
今天主要说下我为了给mongodb数据库添加authorization,大家应该知道,mongo默认是无auth运行的.这可能是方便小伙伴学习命令吧. 由于之前发布的一个项目,在亚马逊的云上,处于内部 ...
- C#时间日期格式大全
C#时间/日期格式大全,C#时间/日期函数大全 有时候我们要对时间进行转换,达到不同的显示效果 默认格式为:2016-7-1 14:33:34 如果要换成成201607,07-2016,2016-7- ...
- IntelliJ IDEA中怎么查看文件中所有方法?
可以使用快捷键ALT + 7打开左侧的Structure查看当前文件中的所有方法. 来自为知笔记(Wiz)
- NTFS文件系统简介(转载)
原文地址:http://www.cnblogs.com/watertao/archive/2011/11/28/2266595.html 1.简介 NTFS(New Technology File S ...
- HDU3415
题目大意: 给出一个有N个数字(-1000..1000,N<=10^5)的环状序列,找出一个长度不大于k的连续子序列,使其和最大. 分析: 我们可以将环状序列从某处切开,变成一行,然后复制前n- ...
- S3C2440之MMU
转自:http://blog.chinaunix.net/uid-23193900-id-3187782.html 1.MMU简介 MMU(Memory Management Unit),内存管 ...
- 黄聪:手机移动站Web响应式开发工具Viewport Resizer插件(360浏览器、谷歌Chrome浏览器兼容)
插件作用: 移植自@MalteWassermann的脚本,一个可以测试响应式布局的chrome扩展. 插件截图: 插件下载地址(需FQ): https://chrome.google.com/webs ...