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. 我的wmware

    1.vmware 网络连接方式 NAT 模式: 虚拟机的IP 是由NAT分配的,电脑环境无论如何变化,都不会影响虚拟机 好处:在家.学校.公司,连接虚拟机都可以使用相同的ip地址 桥接模式: 只要更换 ...

  2. 菜鸟笔记 -- Chapter 1 计算机从0到1

    进入20世纪第二个十年,计算机已经成为生活中一个必不可小的工具了,但我们真的了解计算机吗?计算机有哪些部分构成?不同的计算机又可以做什么样的事情呢?我们的PC和用来做加减乘除的计算器都属于计算机范畴吗 ...

  3. [Oracle]分区索引

    上一节学习了分区表,接着学习分区索引. (一)什么时候对索引进行分区 · 为了避免移动数据时重建整个索引,可对索引分区,在重建索引时,只需重建与数据分区相关的索引: · 在对分区表进行维护时,为了避免 ...

  4. iOS 通用缓存:HanekeSwift

    iOS 通用缓存:HanekeSwift Haneke 是个采用 Swift 编写的轻量级 iOS 通用缓存.示例: 初始化一个数据缓存: let cache = Cache<NSData> ...

  5. rest_framework--RESTful规范

    #####RESTful规范##### 一.什么是restful restful其实就是一种软件架构风格,跟技术毫无关系.是一种面向资源编程的方法. 说起面向资源编程,我想起了之前了解到的面向过程编程 ...

  6. CentOS7——vi编辑保存

    按ESC键 跳到命令模式,然后: :w 保存文件但不退出vi :w file 将修改另外保存到file中,不退出vi :w! 强制保存,不推出vi :wq 保存文件并退出vi :wq! 强制保存文件, ...

  7. 【原创】从 列表的重复 到 用sum展开二层嵌套列表将子元素合并

      转载请注明出处:https://www.cnblogs.com/oceanicstar/p/9517159.html     ★像R语言里头有rep函数可以让向量的值重复,在python里面可以直 ...

  8. 如何安全地跨窗体调用Timer控件 从一个窗体调用控制另外一个窗体的控件

    具体的情况是Form1中有一个Timer2时钟,Timer2时钟事件弹出Warning窗体,点击Warning窗体上面的按钮,重新激活一下Form1中的Timer2.从而实现了从一个窗体调用另外一个窗 ...

  9. [MIP]mip-script组件自定义 JS 代码使用限制

    自mip升级v2版本后,多了一个mip-script组件,很多人就都以为可以写自定义js代码了!然并卵,MIP2页中还是一样不允许自定义javascript代码,所有的交互须通过组件实现. 引用官方说 ...

  10. jquery 节点

    $("ul li:eq(3)")  // 元素的index位置工0开始计数,所以这里的3实际为第4个元素 $("ul").find("li" ...