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 ...
随机推荐
- php curl详细说明
CURLOPT_RETURNTRANSFER 选项: curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 如果成功只将结果返回,不自动输出任何内容. 如果失败返回F ...
- .Net HttpPost的发送和接收示例代码
/// <summary> /// 模拟http 发送post或get请求 /// </summary> /// <param name="Url"& ...
- js 监听整个页面的回车事件
JS监听整个页面的回车事件 <script type="text/javascript"> document.onkeydown=keyDownSearch; ...
- 解决“未启用当前数据库的 SQL Server Service Broker,因此查询通知不受支持。如果希望使用通知,请为此数据库启用 Service Broker”错误
转自:http://blog.csdn.net/andrew_wx/article/details/6947317 解决办法: ALTER DATABASE 数据库名称 SET NEW_BROKER ...
- FileDataSource java的文件操作
FileDataSource:(javax.activation.FileDataSource.FileDataSource(File file)) FileDataSource 类实现一个封装文件的 ...
- MongoDB shell 格式化
直接的方法: db.collection.find().pretty(); 如果想要所有的查询都格式化,可以执行: echo "DBQuery.prototype._prettyShell ...
- java读取properties文件的内容
获得properties文件中某个key对应着的value // 路径名称 + properties的名称,不要“properties” private static final String BUN ...
- VBA 插入一行保留样式
Rows(processingRow).Insert ' 在指定的行数processingRow处插入一行 Rows(processingRow - 1).Select ' 选择上一行的整行 Sele ...
- Python爬虫(图片)编写过程中遇到的问题
最近我突然对网络爬虫开窍了,真正做起来的时候发现并不算太难,都怪我以前有点懒,不过近两年编写了一些程序,手感积累了一些肯定也是因素,总之,还是惭愧了.好了,说正题,我把这两天做爬虫的过程中遇到的问题总 ...
- Python基础教程【读书笔记】 - 2016/7/19
希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第八波:第4章 字典:当索引不好用时 将学到一种通过名字引用值的数据结构,这种结构类型称为映射mapping.字典是P ...