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 ...
随机推荐
- CreateEvent,OpenEvent成功后 是否需要::CloseHandle(xxx); 避免句柄泄漏
bool bExist = false; HANDLE hHandle = ::CreateEvent(NULL, FALSE, FALSE, L"Global\\xxxxx_name ...
- 请求http服务
①服务方法 [HttpGet]//get服务 public JsonResult GetUserName(int id) { try { IXiao_UserBLL bll = new Xiao_Us ...
- javascript函数apply和call
apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性. Function.apply(obj,args)方法能接收两个参数obj:这个对象将代替Function类里this对象args:这 ...
- Android SDK与API版本的对应关系
看教程.开发Android程序等很多地方,需要设置Android SDK的版本,而其要我们写的却是API版本的数字, 为了方便查看 Android SDK与API版本的对应关系 我在SDK Manag ...
- VM 映像
让我们一起欢呼吧!随着最近Microsoft Azure运行时的发布,我们非常高兴地宣布发布 OS映像的继承性产品:新 VM映像.等一下-有些人可能会觉得这听起来有点耳熟.没错,一个月前在旧金山 ...
- Windows Azure Camp---漫步云端,创意无限
不再需要一系列繁杂的网银密码,一键搞定所有的支付:与朋友约会时通过实时分享地理位置迅速找到对方,这些都可以在WindowsAzure平台得以实现.在刚刚结束的2013年微软学生夏令营中,来自全国30所 ...
- openssl命令行Base64编解码
openssl对base64编解码的规范支持较差,用它编解码的结果别的语言如php处理很不方便,注意的几点整理如下 1,如果php加密结果做base64编码长度小于64,则需要添加一个换行符opens ...
- Android 标签控件
版本号:1.0 日期:2014.7.24 版权:© 2014 kince 转载注明出处 在有的应用中可能须要设置一些标签来方便用去去查询某些信息,比方手机助手或者购物软件之类都会有一些标签. ...
- Node.js笔记2
入门二 5. 事件 Node.js中所有的异步I/O操作完成时都会发送一个事件到事件队列. Events 事件模块 `events.EventEmitter` 简单用法: var EventEmitt ...
- [原创] ASP.NET WEBAPI 接入微信公众平台 总结,Token验证失败解决办法
首先,请允许我说一句:shit! 因为这个问题不难,但是网上有关 ASP.NET WEBAPI的资料太少.都是PHP等等的. 我也是在看了某位大神的博客后有启发,一点点研究出来的. 来看正题! 1.微 ...