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

For example,
a = "11"
b = "1"
Return "100".

bool isAllZero(string a)
{
for (int i = 0; i < a.length(); ++i) {
if (a[i] != '0')return false;
}
return true;
}
string stringxor(string a, string b) {
string res="";
int al = a.length()-1;
int bl = b.length()-1;
for (;al >=0 && bl >= 0; al--,bl--) {
char tmp = '0' + (a[al]-'0')^(b[bl]-'0');
res = tmp + res;
}
if (al >= 0) res = a.substr(0,al+1) + res;
if (bl >= 0) res = b.substr(0,bl+1) + res;
return res;
} string stringand(string a, string b) {
string res="";
int al = a.length()-1;
int bl = b.length()-1;
for (;al >=0 && bl >= 0; al--,bl--) {
char tmp = '0' + ((a[al]-'0')&(b[bl]-'0'));
res = tmp + res;
}
return res;
} string addBinary(string a, string b) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (a == "") return b;
if (b == "") return a; string xorres = "";
string andres = "";
do {
xorres = stringxor(a,b);
andres = stringand(a,b);
andres += '0';
a = xorres;
b = andres;
} while (!isAllZero(b)); int i = 0;
for (; i < a.length(); ++i)
if (a[i] != '0') break;
if (i >= a.length())
a = "0";
else
a = a.substr(i);
return a; }

leetcode_question_67 Add Binary的更多相关文章

  1. leetcode解题:Add binary问题

    顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...

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

    Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...

  3. Add Binary

    Add Binary https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (als ...

  4. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

  5. 67. Add Binary【LeetCode】

    67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...

  6. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  7. 2016.6.21——Add Binary

    Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...

  8. LeetCode: Add Binary 解题报告

    Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...

  9. leetcode笔记:Add Binary

    一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...

随机推荐

  1. poj 3252 Round Numbers 数位dp

    题目链接 找一个范围内二进制中0的个数大于等于1的个数的数的数量.基础的数位dp #include<bits/stdc++.h> using namespace std; #define ...

  2. PyCrpyto windows安装使用方法

    PyCrypto - The Python Cryptography Toolkit PyCrypto是一个免费的加密算法库,支持常见的DES.AES加密以及MD5.SHA各种HASH运算. ---- ...

  3. Java Buffer

    1.1 NIO Buffers - Class java.nio.Buffer NIO data transfer is through the so-called buffers implement ...

  4. 通过一个模拟程序让你明白WCF大致的执行流程

    原文http://www.cnblogs.com/artech/archive/2011/12/07/wcf-how-to-work.html 在<通过一个模拟程序让你明白ASP.NET MVC ...

  5. 51nod 1237 最大公约数之和 V3(杜教筛)

    [题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 [题目大意] 求[1,n][1,n]最大公约数之和 ...

  6. thoughtbot/capybara-webkit

    thoughtbot/capybara-webkit A capybara driver that uses WebKit via QtWebKit. Qt Dependency and Instal ...

  7. swith 语句详解

    switch 语句的格式: switch ( 整型或字符型变量 ) { case 变量可能值1 :   分支一; break; case 变量可能值2 :   分支二; break; case 变量可 ...

  8. ios 中生成二维码和相册中识别二维码

    iOS 使用CIDetector扫描相册二维码.原生扫描 原生扫描 iOS7之后,AVFoundation让我们终于可以使用原生扫描进行扫码了(二维码与条码皆可)AVFoundation可以让我们从设 ...

  9. iOS开发获取缓存文件的大小并清除缓存

    移动应用在处理网络资源时,一般都会做离线缓存处理,其中以图片缓存最为典型,其中很流行的离线缓存框架为SDWebImage. 但是,离线缓存会占用手机存储空间,所以缓存清理功能基本成为资讯.购物.阅读类 ...

  10. uva 10548 - Find the Right Changes(拓展欧几里得)

    题目链接:uva 10548 - Find the Right Changes 题目大意:给定A,B,C,求x,y,使得xA+yB=C,求有多少种解. 解题思路:拓展欧几里得,保证x,y均大于等于0, ...