2016.6.21——Add Binary
Add Binary
本题收获:
对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.
1.string中默认每个元素为char型
2.从int型转化为char型 s[i] - '0'
从char型转化为int型 s[i] + '0'
3.char型和int型相加时按上式会报错 s = (char)(s[i] + '0') +s
s = (char)(s[i] + '0') +s 这样的写法 每次新的s(由(char)(s[i] + '0'))会放在左边,(加的s为老的s)
s += (char)(s[i] + '0') 每次新的s会放在右边
注意高低位来决定“+”的位置
4.c += i >= 0 ? a[i] - '0' : 0;这个语句的执行是 c += (i >= 0 ? a[i] - '0' : 0); 首先执行判断语句 ? :,在执行表达式c +=>
5.stray '\343' in program leetcode中报错,将报错行的空格全部删除,需要在重新打上(在写代码时可能出现全角符号的值或者是空格,好好检查下,或者重新输入)
题目:
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
思路:
我的思路:每位相加,但是没想出具体怎么做,其实这样行不通
leetcode:利用中间int型数,每次加两个数的相同位,然后在转化为string型,产生的进位保留加到下次循环。
代码:思路非常棒
class MyClass
{
public:
string addBinary(string a, string b)
{
string s;
long c = ;
int i = a.size() - , j = b.size() - ; while (i >= || j >= || c ==) //注意循环条件 || c == 1
{
c += i >= ? a[i] - '' : ; //string默认的为char型
i--;
c += j >= ? b[j] - '' : ; //从char型到int型 -'0'
j--;
s = (char)((c % ) + '') +s ; //从int型到char型 +'0'
c = c / ; //注意什么时候% ,什么时候 /
} return s;
}
};
我的测试代码:
// Add Binary.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "iostream"
#include "string"
using namespace std; class MyClass
{
public:
string addBinary(string a, string b)
{
string s;
long c = ;
int i = a.size() - , j = b.size() - ; while (i >= || j >= || c ==) //注意循环条件
{
c += i >= ? a[i] - '' : ; //string默认的为char型
i--;
c += j >= ? b[j] - '' : ; //从char型到int型 -'0'
j--;
s = (char)((c % ) + '') +s ; //从int型到char型 +'0'
c = c / ;
} return s;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
string a, b, res;
cin >> a;
cin >> b;
MyClass solution;
res = solution.addBinary(a, b);
cout << res << endl;
system("pause");
return ;
}
2016.6.21——Add Binary的更多相关文章
- FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM
FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM T.. = Timeline support 支持时间轴 .S. = Slice t ...
- Technical Committee Weekly Meeting 2016.06.21
Meeting time: 2016.June.21 1:00~2:00 Chairperson: Thierry Carrez Meeting summary: 1.Add current hou ...
- Murano Weekly Meeting 2016.06.21
Meeting time: 2016.June.21 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: 1. ...
- 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 ...
- ”耐撕“团队 2016.3.21 站立会议3 2 1 GO!
”耐撕“团队 2016.3.21 站立会议 时间:2016.3.21 ① 17:20-17:45 ②17:55-18:10 总计40分钟 成员: Z 郑蕊 * 组长 (博客:http://www ...
- 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 ...
随机推荐
- delphi的ActionToolBar控件的使用 [问题点数:50分]
delphi那些默认图标在哪里就是那些Text,Label,Checkbox,显示在palette上面的那些图标. 上面的按钮是在ActionManager中添加的,让后拖到ActionToolBar ...
- MachineLearning Exercise 4 :Neural Networks Learning
nnCostFunction 消耗公式: a1 = [ones(m,) X]; z2 = a1*Theta1'; pre = sigmoid(a1*Theta1'); a2 = [ones(m,) p ...
- 前端基础:HTTP 状态码详解
HTTP 状态码详解 1xx(信息类):表示接收到请求并继续处理 100 客户端应当继续发送请求.这个临时响应是用来通知客户端他的部分请求已经被服务器接收,且仍未被拒绝.客户端应当继续发送请求的剩余部 ...
- QProcess 进程调用
1. 调用方的接口: void QProcess::start(const QString &program, const QStringList &arguments, OpenMo ...
- Spring(2):Spring Ioc
1.下载spring-framework-3.2.0 完整包下载路径: https://repo.spring.io/webapp/#/artifacts/browse/tree/Properties ...
- 状压DP入门详解+题目推荐
在动态规划的题型中,一般叫什么DP就是怎么DP,状压DP也不例外 所谓状态压缩,一般是通过用01串表示状态,充分利用二进制数的特性,简化计算难度.举个例子,在棋盘上摆放棋子的题目中,我们可以用1表示当 ...
- Common Substrings POJ - 3415(长度不小于k的公共子串的个数)
题意: 给定两个字符串A 和 B, 求长度不小于 k 的公共子串的个数(可以相同) 分两部分求和sa[i-1] > len1 sa[i] < len1 和 sa[i-1] < ...
- xsl 文件如何定义 Javascript 函数并且调用
<?xml version='1.0'?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3. ...
- easyui的datebox只显示年月
要求点击easyui的datebox时只显示年月,隐藏日,之前百度了好多,发现有的好麻烦,有的使用没效果,也许自己没理解,改不了.最后老员工帮我搞定了,添加一个fomatter和一个parser函数就 ...
- myeclipse2015修改web项目部署名
在旧版本的myeclipse中修改web项目部署名很方便,直接右键在properties中查找web就能进行修改. 但是myeclipse2015中发现不能直接修改了. 我们可以点击config,或者 ...