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. 小测试 php代理,nginx代理,直接访问对比

    #php proxy total sent request num: 507 total handle read times: 506 506 fetches, 2 max parallel, 2.7 ...

  2. mysql中判断表中是否存在某条记录

    SELECT CASE WHEN EXISTS (SELECT * FROM usergroupmap WHERE groupId = groupIdIn AND userId = v_friendI ...

  3. [互联网面试笔试汇总C/C++-9] 实现赋值运算符函数-剑指offer

    题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: CMyString(char* pData = NULL); CMyStr ...

  4. 关于XPath的基本语法

    关于XPath基础语法 关于XPath基础语法 更详细的请看: XPath语法 XPath 使用路径表达式来选取 XML 文档中的节点或节点集.节点是通过沿着路径 (path) 或者步 (steps) ...

  5. python cmd命令调用

    关于python调用cmd命令: 主要介绍两种方式: 1.python的OS模块. OS模块调用CMD命令有两种方式:os.popen(),os.system(). 都是用当前进程来调用. os.sy ...

  6. 《Pointers On C》读书笔记(第三章 数据)

    1.在C语言中,仅有4种基本数据类型:整型.浮点型.指针和聚合类型(如数组和结构等). 整型家族包括字符.短整型.整型和长整型,它们都分为有符号和无符号两种. 标准规定整型值相互之间大小的规则:长整型 ...

  7. MacOS Apache配置

    仅适用于apache 2.2版本   查看版本 sudo apachectl -v   启动服务器 sudo apachectl start 打开localhost,可以看到内容为“It works! ...

  8. java 构造方法 constructor demo笔记

    demo 地址 http://pan.baidu.com/s/1bo2FG1T package com.ws.study; /** * @author Administrator * */ publi ...

  9. iPhone 6 为何坚持1GB内存?

    原文地址:http://digi.ifeng.com/expert/special/96/#6467378-qzone-1-9015-46cf52f061fd6e814686a918cedcb024 ...

  10. tocken和ticket的数据模型;

    /* jshint -W079 */ /* jshint -W020 */ "use strict"; var _ = require("lodash"); m ...