leetcode_question_67 Add Binary
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的更多相关文章
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
- [LintCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...
- Add Binary
Add Binary https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (als ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- 67. Add Binary【LeetCode】
67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 2016.6.21——Add Binary
Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...
- LeetCode: Add Binary 解题报告
Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...
- leetcode笔记:Add Binary
一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...
随机推荐
- Html页面加回滚
<div class="top-box"> <img src=" class="youlink-img" /><br / ...
- 亲测的一款在线作图神器:ProcessOn
本人近日发现一款作图神器:ProcessOn 它是一款在线的作图工具,完全国产,前台是用HTML5 Canvas加javascript做绘图,后台用java实现数据处理和图片生成, 整站UI基本类似 ...
- jquery取对象数组元素的错误方式
代码如下: <div id="div1"> <span>a</span> <span>b</span> <span ...
- python基础--模块&包
一.模块 1.模块是程序 任何Python程序都可以作为模块导入. 程序的保存也很重要,如果想把程序保存在C:\python (Windows)目录下,需要告诉解释器在哪里寻找模块了. >> ...
- Hadoop2.0安装
http://blog.csdn.net/samhacker/article/details/18802223 http://blog.csdn.net/crazyhacking/article/de ...
- java 如何得到ISO 8601 时间格式
http://blog.csdn.net/brightleo/article/details/7457004 public class DateUtil { public static String ...
- poj 2228 Naptime dp
这个题目的状态还是比较好想的,dp[i][j]表示已经睡了i个时段,最后睡在j时段的最优值,但是需要处理环的情况,我的做法是算两次,第一次不处理环,第二次强制性要求第一个时段需要睡,然后查看dp[m] ...
- 2015 11 27编写JAVA程序
在任意文件下 ,建立一个文本文档,更改其txt格式为java格式, 打开此程序的同时打开eclipse可编写代码. public class 文件名{ public static void main( ...
- The Highest Mark(01背包)
The Highest Mark Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- VS2015操作Oracle数据需要做那些设置?
1>在oracle网上下载:ODP.NET 2> 要根据自己的oracle 数据32bit/64bit,选择下载. 3> 根据提示配置tnsnames.ora文件. # alias ...