Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"
 class Solution {
public:
string addBinary(string a, string b) {
int la = a.length();
int lb = b.length();
string res = "";
int car = ;
if(la <= lb) {
for(int i = la - ; i >= ; i--) {
if(a[i] == '' && b[lb - (la - i)] == '') {
if(car == )
res = '' + res;
else {
res = '' + res;
car = ;
}
}
else if(a[i] != b[lb - (la - i)]) {
if(car == )
res = '' + res;
else
res = '' + res;
}
else {
if(car == ) {
res = '' + res;
car = ;
}
else
res = '' + res;
}
}
for(int i = lb - la - ; i >= ; i--) {
if(b[i] == '') {
if(car == )
res = '' + res;
else
res = '' + res;
}
else {
if(car == ) {
res = '' + res;
car = ;
}
else
res = '' + res;
}
}
if(car == ) {
res = '' + res;
}
return res;
}
else if(lb < la) {
for(int i = lb - ; i >= ; i--) {
if(b[i] == '' && a[la - (lb - i)] == '') {
if(car == )
res = '' + res;
else {
res = '' + res;
car = ;
}
}
else if(b[i] != a[la - (lb - i)]) {
if(car == )
res = '' + res;
else
res = '' + res;
}
else {
if(car == ) {
res = '' + res;
car = ;
}
else
res = '' + res;
}
}
for(int i = la - lb - ; i >= ; i--) {
if(a[i] == '') {
if(car == )
res = '' + res;
else
res = '' + res;
}
else {
if(car == ) {
res = '' + res;
car = ;
}
else
res = '' + res;
}
}
if(car == ) {
res = '' + res;
}
return res;
}
}
};

LeetCode - 67. Add Binary(4ms)的更多相关文章

  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. LeetCode 67. Add Binary

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

  4. Java [Leetcode 67]Add Binary

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

  5. (String) 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 二进制加法

    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. C#基础 一(方法详解)

    需要知道:类和方法的关系 方法和参数修饰符 自定义方法可以有或没有参数,也可以有或没有返回值.可以被各种关键字(static.virtual.public.new等)修饰以限制其行为. C#参数修饰符 ...

  2. JavaScript函数变量作用域

    变量作用域 在JavaScript中,用var申明的变量实际上是有作用域的. 如果一个变量在函数体内部申明,则该变量的作用域为整个函数体,在函数体外不可引用该变量. 如果两个不同的函数各自申明了同一变 ...

  3. 掘金上发现的有趣web api

    本篇文章主要选取了几个有趣且有用的webapi进行介绍,分别介绍其用法.用处以及浏览器支持度 page lifecycle onlineState(网络状态) device orientation(陀 ...

  4. 课时70.id选择器和类选择器(理解)

    1.id和class的区别? id相当于人的身份证不可以重复 class相当于人的名称可以重复 1.2 一个HTML标签只能绑定一个id名称 一个HTML标签可以绑定多个class名称 2.id选择器 ...

  5. ORA-12154/ORA-12560 可以尝试的解决办法

    WIN10  本机安装ORACLE数据库和ORACLE客户端后,使用PL/SQL 登陆提示错误ORA-12154 和ORACLE-12560, 在检查了本机的注册表.环境PATH路径.tnsnames ...

  6. c#本地缓存实现

    用了一段时间java,java实现服务端程序很简单,有很多公共开源的组件或者软件.但是c#的很少. 现在准备自己写点东西,学习下新的东西,总结下c#的内容以及我们经常用的内容,抽离成类,组件,模型.方 ...

  7. Less 常用基础知识

    LESS 中的注释 也可以额使用css 中的注释(/**/) 这种方式是可以被编译出来的. 也可以使用// 注释 不会被编译的 变量 声明变量的话一定要用@开头 例如:@变量名称:值: @test_w ...

  8. js关于if(''==0)

    在js当中,如下注意 if(''==0){ alert("空字符代表false"); } 空字符串代表false 0代表false false==false 结果就为true了

  9. ABAP术语-Key Field

    Key Field 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/28/1084421.html Field which is part o ...

  10. tomcat6添加服务

    Mysql在导入大量数据的时候就要把tomcat添加成服务 添加服务 在DOS界面下,进入Tomcat解压目录的bin目录 service.bat install