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的更多相关文章

  1. LeetCode 67. Add Binary (二进制相加)

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

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

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

  3. Java [Leetcode 67]Add Binary

    题目描述: Given two binary strings, return their sum (also a binary string). For example,a = "11&qu ...

  4. (String) leetcode 67. Add Binary

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

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

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

  6. LeetCode - 67. Add Binary(4ms)

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

  7. leetcode 67. Add Binary (高精度加法)

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

  8. LeetCode 67 Add Binary(二进制相加)(*)

    翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...

  9. leetCode 67.Add Binary (二进制加法) 解题思路和方法

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

随机推荐

  1. TKinter之窗口美化 窗口大小、图标等

    设置窗口大小.设置窗口标题.设置窗口图标 效果图: 代码示例: #!/usr/bin/env python # _*_ coding:utf-8 _*_ from Tkinter import * r ...

  2. css之cursor,float

    鼠标形状: 在html中可以任意定义各个标签的显示形状,也可以此用来做些标签显示假像. <body> <p style="cursor: pointer"> ...

  3. WCF学习心得----(四)服务承载

    WCF学习心得----(四)服务承载 这一章节花费了好长的时间才整理个大概,主要原因是初次接触这个东西,在做练习实践的过程中,遇到了很多的问题,有些问题到目前还没有得以解决.所以在这一章节中,有一个承 ...

  4. WPF Image控件使用本地图片

    BitmapImage bi = new BitmapImage(); // BitmapImage.UriSource must be in a BeginInit/EndInit block. b ...

  5. android studio提示unable to run mksdcard sdk

    如题,android studio提示unable to run mksdcard sdk sudo apt-

  6. 移动Oracle的用户表空间文件方法

    原文:http://www.linuxidc.com/Linux/2014-07/104702.htm 1.以sys用户登录        sqlplus /nologSQL>connect s ...

  7. 黄聪:jquery.confirm弹出确认消息

    1.插件介绍 该确认框的默认样式如: 1.1.插件默认参数 // 默认参数 $.confirm.defaults = { // 样式 css: "http://static.qianduan ...

  8. SchemaExport的使用

    @Test public void testCreateDB(){ Configuration cfg = new Configuration().configure(); SchemaExport ...

  9. CentOs of Tomcat commands

    1.启动tomcat 进入tomcat目录的bin目录下 执行 [root@iZ253lxv4i0Z bin]# ./startup.sh Using CATALINA_BASE: /usr/loca ...

  10. unity客户端与c++服务器之间的简单通讯_1

    // 服务器 # pragma once using namespace std; # include <iostream> # include <string> # incl ...