Lintcode: Add Binary
C++
class Solution {
public:
/**
* @param a a number
* @param b a number
* @return the result
*/
string addBinary(string& a, string& b) {
// Write your code here
if (a == "") {
return b;
}
if (b == "") {
return a;
}
string result;
int carry = ;
int ai, bj, sum;
char val;
for (int i = a.size()-, j = b.size()-; i >= || j >= ; i--, j--) {
ai = i >= ?a[i]-'':;
bj = j >= ?b[j]-'':;
sum = ai+bj+carry;
val = sum%?'':'';
carry = sum/;
result.insert(result.begin(), val);
}
if (carry == ) {
result.insert(result.begin(), '');
}
return result;
}
};
Lintcode: Add Binary的更多相关文章
- [LintCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
- Add Binary
Add Binary https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (als ...
- LeetCode 面试:Add Binary
1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...
- 67. Add Binary【LeetCode】
67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 2016.6.21——Add Binary
Add Binary 本题收获: 对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.1.string中默认每个元素为char型 2.从i ...
- LeetCode: Add Binary 解题报告
Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "1 ...
- leetcode笔记:Add Binary
一.题目描写叙述 Given two binary strings, return their sum (also a binary string). For example, a = "1 ...
随机推荐
- Download Hacking Team Database from torrent using magnet link
20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送) 国内私募机构九鼎控股打造,九鼎投资是在全国股 ...
- Chrome 如何知道网站启用了SPDY 协议?
地址栏输入chrome://net-internals/#spdy 在host后查看协议,google和dropbox用https协议的开启了 3. 也可以通过安装插件来查看(SPDY Indicat ...
- 【Devops】【docker】【CI/CD】Jenkins源代码管理 添加gitlab项目地址,报错Failed to connect to repository : Error performing command: ls-remote -h git@192.168.92.130:8090/root/swapping.git HEAD
Jenkins源代码管理 添加gitlab项目地址 报错如下: Failed to connect to repository : Error performing command: ls-remot ...
- WordPress主题开发:开启feed功能
开启feed功能 步骤一:在模版文件的<head></head>元素中添加wp_head()函数,且wp_head()函数要放在</head>标签之前,而且紧邻&l ...
- PetaPoco:SkipTake 和 Page 中的 OrderBy 子句不支持 “[]” 的解决办法
PetaPoco 的 SkipTake 和 Page 方法内部采用了内联视图,而内联视图是不支持 OrderBy 的,因此 PetaPoco 对传入的 SQL 进行分析,对 OrderBy 子句进行分 ...
- 用代码获取APP启动页图片
用代码获取APP启动页图片 源码 - swift // // AppleSystemService.swift // Swift-Animations // // Created by YouXian ...
- python 多线程日志切割+日志分析
python 多线程日志切割+日志分析 05/27. 2014 楼主最近刚刚接触python,还是个小菜鸟,没有学习python之前可以说楼主的shell已经算是可以了,但用shell很多东西实现起来 ...
- RV32A指令集
RV32A指令包括两类:AMO(atomic memory operation)指令,Load-Reserved/Store-Conditional指令 Category Fmt RV32I base ...
- 第二章 BIO与NIO
<netty权威指南>读书笔记 一.BIO 1.服务端程序: package bio; import java.io.BufferedReader; import java.io.IOEx ...
- EF6源码学习-准备篇
现在对于.net开发人员来说EF已经很流行了,虽然我在2010年的时候就用过EF,也看过几本书,但是还没有仔细研究EF的code, 曾经也尝试阅读EF5的源代码,后来由于时间关系也没有坚持住.现在计划 ...