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

Have you met this question in a real interview?

Yes
Example

a = 11

b = 1

Return 100

LeetCode上的原题,请参见我之前的博客Add Binary

class Solution {
public:
/**
* @param a a number
* @param b a number
* @return the result
*/
string addBinary(string& a, string& b) {
string res = "";
int m = a.size() - , n = b.size() - , carry = ;
while (m >= || n >= ) {
int p = m >= ? a[m--] - '' : ;
int q = n >= ? b[n--] - '' : ;
int sum = p + q + carry;
res = to_string(sum % ) + res;
carry = sum / ;
}
return carry == ? "" + res : res;
}
};

[LintCode] Add Binary 二进制数相加的更多相关文章

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

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

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

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  3. Lintcode: Add Binary

    C++ class Solution { public: /** * @param a a number * @param b a number * @return the result */ str ...

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

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

  5. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  6. leetcode解题:Add binary问题

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

  7. leetcode笔记:Add Binary

    一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...

  8. [LeetCode] 415. Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  9. LeetCode 面试:Add Binary

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

随机推荐

  1. update comboBox

    /// <summary> /// AutoCompleteComboBox /// </summary> public class AutoCompleteComboBox ...

  2. LeetCode——Reverse Integer(逆置一个整数)

    问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321   Ha ...

  3. ios程序播放音频文件

    1.控制器代码 #import "ViewController.h" #import <AVFoundation/AVFoundation.h> #import &qu ...

  4. Android Fragment学习笔记(二)----Fragment界面添加和管理

    Fragment界面添加 了解过fragment的生命周期等简单知识,于是去看官方文档来了解更多相关内容,要添加fragment到我们的UI界面中,给出了两种常用的方法,第一个是在activity的布 ...

  5. 使用Hue上传hive数据

    大概逻辑是先上传hdfs数据,然后创建hive外部表,关联到hdfs上传数据的位置. 截图比较概要,但是用起来很简单 1.创建路径和上传文件 2.创建外部表

  6. 启动windows的服务--《用delphi开发共享软件》-15.2桌面提示器

    在dos 下用命令启动一个服务:NET START "Windows Desktop Reminder" 一下为用delphi启动服务: Function RunProcess(s ...

  7. tonado框架的列表嵌套 (template中for循环嵌套)

            学习了tonado框架,渐渐开始明白模板的转换,以后肯定还会遇到很多问题... 功能描述:       页面显示读取的数据库父导航名称,再通过嵌套列表将子导航名称和地址查询出来,返回到 ...

  8. 【面试题】BD

    一面: 自我介绍,简单介绍项目: /***********发现项目没什么可问的,然后开始各种基础知识o(╯□╰)o************/ 内存结构,低地址,高地址: STL底层实现,set是否有序 ...

  9. RColorBrewer包---R语言的配色方案

    // RColorBrewer包介绍 RColorBrewer包提供了3套很好的配色方案.用户只需要指定配色方案的名称,就可以用包中的brewer.pal()函数生成颜色.这3套配色方案包括: 连续型 ...

  10. jquery layer弹出层插件

    http://www.51xuediannao.com/js/jquery/jquery_layer/layer.html