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

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

class Solution {
public:
string addBinary(string a, string b) {
int carry = , alen = a.length(),blen = b.length();
string res="";
if(alen > blen) {b=string(alen-blen,'')+b;blen = alen;}
if(alen < blen) {a=string(blen-alen,'')+a;alen = blen;}
for(int i = alen-; i>= ; -- i){
int num =(a[i]-'')+(b[i]-'')+carry;
carry = ;
if(num >= ) {num-=;carry=;}
res+=''+num;
}
if(carry) res+='';
reverse(res.begin(),res.end());
return res;
}
};

Leetcode Add Binary的更多相关文章

  1. LeetCode: Add Binary 解题报告

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

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

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  3. [leetcode]Add Binary @ Python

    原题地址:https://oj.leetcode.com/problems/add-binary/ 题意: Given two binary strings, return their sum (al ...

  4. LeetCode——Add Binary

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  5. LeetCode Add Binary |My Solution

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  6. [Leetcode] add binary 二进制加法

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

  7. LeetCode Add Binary 两个二进制数相加

    class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...

  8. LeetCode 面试:Add Binary

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

  9. leetcode解题:Add binary问题

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

随机推荐

  1. Hybrid App是如何实现网页语言与程序语言的混合?谁占主体?

    [编者按]本文作者@徐珂铭,一位看好Html5的移动互联网的从业人士.喜爱玩技术,会点JAVA.HTML及CSS,有自己的想法及姑且能表达想法的文字,因此有了自己的文章. 基于HTML5的Web Ap ...

  2. HTML5学习之跨文档传输消息(七)

    新标准中提供了文档之间直接的消息传输API.而且不限制跨域消息传递! 发送消息使用的是Window对象的postMessage(data,targetURL)方法就可以了,但给哪个window对象发送 ...

  3. [LeetCode] Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  4. 攻城狮在路上(叁)Linux(二十)--- Linux磁盘格式化

    磁盘完成分区之后,进行格式化,生成文件系统. 命令格式: mkfs [-t 文件系统格式] 设备文件名  <== 使用 mkfs [Tab][Tab] 可以查看linux支持的文件系统格式 示例 ...

  5. php开发(TP框架使用)

    由于最近玩了PHP,我向来有个原则,学一门服务端语言至少得玩两个框架,前段时间用PHP写了些demo+小项目,看见身边有人在使用TP,于是乎鼓捣学习学习.如何学,无非也就是做个小demo:就目前看来现 ...

  6. C# SMTP邮件发送 分类: C# 2014-07-13 19:10 333人阅读 评论(1) 收藏

    邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...

  7. java 杂物间 (二) Spring Web

    需要明确记住的继承关系

  8. hdu 5366 组合数 *

    考虑放1个,2个....的情况,相加就是最后的结果 #include<cstdio> #include<iostream> #include<algorithm> ...

  9. [JavaCore] 不错的Java基础学习资料-持续更新

    容易弄混的JAVA基础知识: http://www.iteye.com/topic/943647 [总结]String in Java: http://www.iteye.com/topic/5221 ...

  10. linux文本模式下使用PPPOE拨号ADSL上网的方法

    转自:http://www.myzhenai.com.cn/post/945.html 转载请注明出处:http://www.myzhenai.com/thread-15431-1-1.html ht ...