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. 提供一段Excel获取Title的标题,类似于A、AA、AAA,我们操作Excel的时候通常根据次标题来获取一定的操作范围。

    /******************************************** FormatExcelColumTitle Purpose Get excel title like &qu ...

  2. 小鱼提问2 属性访问器中get,set再用public修饰行吗,private呢?

    /// <summary> /// 是否有一个用户正在连接服务器中 /// </summary> public bool IsConnectting { get { retur ...

  3. .NET DataGrid 导出Excel 无分页

    #region 导出Excel // protected void BtnExcelClick(object sender, EventArgs e) { ToExcel(); } public vo ...

  4. Weblogic安装NodeManager

    http://blog.sina.com.cn/s/blog_6ed936400100ytdo.html

  5. HDU 4436 str2int(后缀自动机)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4436 [题目大意] 给出一些字符串,由0~9组成,求出所有不同子串的和. [题解] 将所有字符串添 ...

  6. 给即将面临Noip的二班同学

    给即将面临Noip的二班同学: 我们共同走过了一年,在这里,真正认识彼此…… 失落过,但更多是欢笑…… 或许我们班的信息学竞赛承受着巨大的压力,但正因为这样,我们才学会了坚持:或许我们得不到他人的认可 ...

  7. Mysql文件太大导入失败解决办法总结

    Mysql文件太大导入失败解决办法总结 在使用phpmyadmin导入数据库的时候可能会碰到由于数据库文件太大而无法导入的问题! 英文提示如下:File exceeds the maximum all ...

  8. Java程序栈信息文件中的秘密(五)

    最近发现在使用jstack工具在导出Java应用的线程栈时有一个小小的窍门,比如Linux环境上有一个用户为appuser,假如以这个用户启动了一个Java进程B,如果想要导出进程B的线程栈,则必须切 ...

  9. Docker之dockerfile

    一.什么是dockerfile Docker通过对于在dockerfile中的一系列指令的顺序解析实现自动的Image的构建: 通过使用build命令,根据dockerfile的描述来构建镜像: bu ...

  10. 解决 Tomcat reload WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [] registered the JDBC driver [com.mysql.jdbc.Driver] but fail

    转自:http://www.cnblogs.com/interdrp/p/5632529.html 我的错误如下: 06-Sep-2016 18:57:10.595 WARNING [localhos ...