[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 ...
随机推荐
- Windows+Git+TortoiseGit+COPSSH 安装图文教程
转自:http://blog.csdn.net/aaron_luchen/article/details/10498181 准备工作: 1. Git-1.8.1.2-preview20130201.e ...
- Effective C++ 之 Item 4:确定对象被使用前已先被初始化
Effective C++ Chapter 1. 让自己习惯C++ (Accustoming Yourself to C++) Item 4. 确定对象被使用前已先被初始化 (Make sure th ...
- Android基本认识
AndroidManifest.xml file missing! 是因为开始想当然的用中文当project名 no launcher activity found 第一次运行出了点问题,no lau ...
- SQLServer 维护脚本分享(07)IO
sp_helpfile --当前数据库文件分配情况 sp_spaceused --当前db空间大小(有时不准) sp_spaceused 'dbo.user' --指定表的空间大小(有时不准) sp_ ...
- mysql注入研究
网址: http://www.jb51.net/article/14446.htm http://www.jb51.net/article/29445.htm
- 深入剖析Java中的装箱和拆箱
深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最基本的东西,再来看一下面试笔试中经常遇到的与装箱 ...
- zepto下加载openbox弹出层
function fnOpenBox(objId,animateD,speed,tween,hide){ var oOpenBox = $(objId); oOpenBox.show(); oOpen ...
- MFC 程序以管理员权限运行
首先,VS打开项目的属性 然后设置如图: 转载自:http://www.cnblogs.com/zzuhjf/archive/2012/09/12/2681548.html
- iOS 安装Cocoapods以及安装第三方库的操作流程
安装cocoapods的流程: 1.打开终端,输入: sudo gem update —system 2.输入密码,稍等 3.gem sources --remove https://rubygem ...
- 转载:Robotium之Android控件定位实践和建议(Appium/UIAutomator姊妹篇)
来源于:http://blog.csdn.net/zhubaitian/article/details/39803857 1. 背景 为保持这个系列的一致性,我们继续用SDK自带的NotePad实例应 ...