[LintCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string).
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 二进制数相加的更多相关文章
- [LeetCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- [LeetCode] 67. Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...
- Lintcode: Add Binary
C++ class Solution { public: /** * @param a a number * @param b a number * @return the result */ str ...
- LeetCode Add Binary 两个二进制数相加
class Solution { public: string addBinary(string a, string b) { if(a==""&&b==" ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
- leetcode笔记:Add Binary
一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...
- [LeetCode] 415. Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
随机推荐
- C++中单例模式
//C++单例模式:指一个类只生成一个对象 #include <iostream> using namespace std; class A{ public: static A* getA ...
- SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax
275. To xor or not to xor The sequence of non-negative integers A1, A2, ..., AN is given. You are ...
- Redis在Windows下的安装和使用
NoSQL简介 介绍redis前,我想还是先认识下NoSQL,即not only sql, 是一种非关系型的数据存储,key/value键值对存储.现有Nosql DB 产品: Redis/Mongo ...
- SQL作业的操作全
--定义创建作业 转自http://hi.baidu.com/procedure/blog/item/7f959fb10d76f95d092302dd.html DECLARE @jobid uniq ...
- python装饰器--@property
@property 考察 Student 类: class Student(object): def __init__(self, name, score): self.name = name sel ...
- asp.net权限控制配置web.config
项目下 有三个文件夹 A,B,C 验正方式是 Forms 验正 我要设置他们的访问权限为, A,匿名可访问 B,普通用户授权后才能访问 C,只允许管理员访问 <configuration> ...
- asp.net项目中通过Web.config配置文件及文件夹的访问权限!
描述:在开发中我们通常会碰到这样的问题,例如:在项目的根目录下面有一个文件或者文件夹需要用户登陆后才能访问.如果用户在没有登录的情况下访问该文件或者该文件夹下面的文件时,直接拦截重定向到对应的登陆页面 ...
- js与jquery异同
大家都知道jquery是js的一个库,很多东西大多数简写了,让js写起来特别的方便.但是对与学习的人来说,最好是先学会了js再去学jquery会更好.在学得过程中你会发现两者实现的原理是差不多的,但是 ...
- checkbox 的全选与全不选
checkbox 的全选与全不选 只需要调用 cekAll.check();方法,这个方法接收两个参数: 参数一: 全选按钮的 id 以字符串的形式写 参数二: 其他 checkbox 的 name ...
- iOS开发实用干货——强化你的Xcode控制台
f(x) 郑秀晶程序员不要整天看代码,偶尔也要看看风景?? www.90168.org先上一张我的Xcode控制台的图片让你们感受一下 酷炫控制台 是不是觉得很酷?不过仅仅是酷还是远远不够的,当你点击 ...