Add Binary <leetcode>
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 len1=a.size();
int len2=b.size();
int c=;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
string s="";
int len=max(len1,len2);
for(int i=;i<len;i++)
{
int t=;
if(i<len1) t+=a[i]-'';
if(i<len2) t+=b[i]-'';
t+=c;
c=t/;
s=(char)(t%+'')+s;
}
if(c>) s=(char)(c+'')+s;
return s;
}
};
Add Binary <leetcode>的更多相关文章
- leetcode解题:Add binary问题
顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...
- [LintCode] Add Binary 二进制数相加
Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...
- 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 ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
随机推荐
- 利用中文数据跑Google开源项目word2vec
一直听说word2vec在处理词与词的相似度的问题上效果十分好,最近自己也上手跑了跑Google开源的代码(https://code.google.com/p/word2vec/). 1.语料 首先准 ...
- unix exec族函数 关于参数的疑惑
问题不出在这几个函数,而在于看后文解释器的时候发现一个很奇妙的问题. #include <unistd.h> int execl(const char *pathname, const c ...
- Java笔记5-修饰符,重载,递归,数组
方法的定义修饰符 返回类型 方法名(参数列表) throws 异常类型列表 { //方法体}==如何来设计一个方法:案例:请根据如下的业务分别设计出方法1.根据给定的年份判断是否是闰年?public ...
- 用正则从html代码中提取图片路径
$str = '<div align="center"> <img src="http://www.99tyg.com/public/images/e8 ...
- JavaScript对象属性赋值操作的逻辑
对象进行属性赋值操作时,其执行逻辑如下所示: 1. 当前对象中是否有该属性?有,进行赋值操作:没有,进行下一步判断. 2. 对象的原型链中是否有该属性?没有,在当前对象上创建该属性,并赋值:有,进行下 ...
- SPOJ QTREE Query on a tree
题意:给一颗n个点的树,有两种操作CHANGE i ti : 把第i条边的权变为tiQUERY a b : 问点a 到 点b 之间的边的最大权 思路:树剖处理边权.由于是边,所以只需要把边权处理到子节 ...
- Python之路,day2-Python基础1
python2 range(20) for i in range(10): print(i) range(1,10) ----->从1开始到9 else: #如果for循环正常结束, 就执行 ...
- Delphi 2010错误 "displayNotification: 内存不够"
打开 Delphi 2010 的时候, 它报一个错误 "displayNotification: 内存不够",从而无法使用 Delphi , 解决办法 摘自http://www.cnblogs.com ...
- <<有效软件测试>> 读书笔记和自己的一些思考
需求阶段 1. 测试人员及早介入,需要彻底了解产品,设计测试过程 * 及早介入,可以了解在开发的过程中需要使用哪些新技术,新的平台, 测试组是否方便进行测试,是否方便进行自动化测试,早期开发和测试应该 ...
- DB设计原则(二)如何拆、分表
不要盲目为了减少表的数量,而使某一张表的字段过多,而导致表冗余列存在