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. gcc扩展语法一:在表达式中的语句和声明

    在GNU C中包含在括号中的复合语句可以作为一个表达式.这就允许你在表达式中使用循环,switch和局部变量. 以前复合语句是包含在大括号中语句序列.在这种构造中,圆括号包围在大括号中.如下面的例子: ...

  2. 课时15.DTD文档声明下(了解)

    W3C的官方网站是W3School,我们可以去官方网站查询DTD文档声明. HTML4.01       Strict  非常严谨的 如果你写了这个DTD文档声明,你就不能写如下样式: <fon ...

  3. 轻量ORM-SqlRepoEx (五) 存储过程操作

    .Net平台下兼容.NET Standard 2.0,一个实现以Lambda表达式转转换标准SQL语句,使用强类型操作数据的轻量级ORM工具,在减少魔法字串同时,通过灵活的Lambda表达式组合,实现 ...

  4. JSP静态包含和动态包含

    JSP中有两种包含: 静态包含:<%@include file="被包含页面"%>: 动态包含:<jsp:include page="被包含页面&quo ...

  5. iOS | TableView的优化

    TableView是iOS组件中最常见.最重要的组件之一,在开发中常常用到,所以对其进行优化是一项必不可少的基本功. 主要从几个最常用的方面来对其优化: 1.重用机制 重用机制是cell最基础的一项优 ...

  6. iOS自动打开闪光灯

    现在好多应有都具备扫码功能,为了减少用户操作,一般会在光线比较暗的时候,自动打开闪光灯: 1.导入头文件 #import <AVFoundation/AVFoundation.h> #im ...

  7. zepto 基础知识(5)

    81.width width() 类型:number width(value) 类型:self width(function(index,oldWidth){....}) 类型:self 获取对象集合 ...

  8. jquery获取周对应的日期

    项目中用到按周显示的功能,找了一个,然后自己修改了一下,留着以后用: 这是代码,要是直接显示的话就把第43行去掉就行了,如果想要得到数据按照自己的想法重新渲染就保留43行,直接看51行,52行就是你要 ...

  9. 在Liunx上搭建FTP并配置用户权限

    伴随着.Net Core的开源,公司前几天上了新的Liunx服务器,我在前几篇文章中介绍了如何搭建环境以及部署.Net Core应用. 然后,今天客户和我说想自己给网站做推广,需要用FTP链接我们的服 ...

  10. Spring常见面试题

    本文是通过收集网上各种面试指南题目及答案然后经过整理归纳而来,仅仅是为了方便以后回顾,无意冒犯各位原创作者. Spring框架 1. 什么是Spring? Spring 是个java企业级应用的开源开 ...