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 ...
随机推荐
- TKinter之窗口美化 窗口大小、图标等
设置窗口大小.设置窗口标题.设置窗口图标 效果图: 代码示例: #!/usr/bin/env python # _*_ coding:utf-8 _*_ from Tkinter import * r ...
- css之cursor,float
鼠标形状: 在html中可以任意定义各个标签的显示形状,也可以此用来做些标签显示假像. <body> <p style="cursor: pointer"> ...
- WCF学习心得----(四)服务承载
WCF学习心得----(四)服务承载 这一章节花费了好长的时间才整理个大概,主要原因是初次接触这个东西,在做练习实践的过程中,遇到了很多的问题,有些问题到目前还没有得以解决.所以在这一章节中,有一个承 ...
- WPF Image控件使用本地图片
BitmapImage bi = new BitmapImage(); // BitmapImage.UriSource must be in a BeginInit/EndInit block. b ...
- android studio提示unable to run mksdcard sdk
如题,android studio提示unable to run mksdcard sdk sudo apt-
- 移动Oracle的用户表空间文件方法
原文:http://www.linuxidc.com/Linux/2014-07/104702.htm 1.以sys用户登录 sqlplus /nologSQL>connect s ...
- 黄聪:jquery.confirm弹出确认消息
1.插件介绍 该确认框的默认样式如: 1.1.插件默认参数 // 默认参数 $.confirm.defaults = { // 样式 css: "http://static.qianduan ...
- SchemaExport的使用
@Test public void testCreateDB(){ Configuration cfg = new Configuration().configure(); SchemaExport ...
- CentOs of Tomcat commands
1.启动tomcat 进入tomcat目录的bin目录下 执行 [root@iZ253lxv4i0Z bin]# ./startup.sh Using CATALINA_BASE: /usr/loca ...
- unity客户端与c++服务器之间的简单通讯_1
// 服务器 # pragma once using namespace std; # include <iostream> # include <string> # incl ...