Leetcode——67. Add Binary
来源:互联网 发布:一淘是淘宝的吗 编辑:程序博客网 时间:2024/05/17 00:58
问题
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 i=0; string res=""; int carryout=0; while(i<a.length()||i<b.length()) { int alast=a.length()-i-1; int blast=b.length()-i-1; if(alast>=0) carryout=carryout+(a[a.length()-i-1]-'0'); if(blast>=0) carryout=carryout+(b[b.length()-i-1]-'0'); res=char (carryout%2+'0')+res; carryout=carryout/2; i++; } if(carryout==1) res="1"+res; return res; }};*
这一题很奇怪的一个地方是:
int alast=a.length()-i-1; int blast=b.length()-i-1; if(alast>=0) carryout=carryout+(a[a.length()-i-1]-'0'); if(blast>=0) carryout=carryout+(b[b.length()-i-1]-'0');
这部分代码换成
if(a.length()-i-1>=0) carryout=carryout+(a[a.length()-i-1]-'0'); if(b.length()-i-1>=0) carryout=carryout+(b[b.length()-i-1]-'0');
之后就是运行不对,放在VS2015下跑,
中间打印`
cout<<b.length()-i-1<<endl
得出很大的一个数
但是一旦把
int blast=b.length()-i-1;cout<<blast<<endl
得出的结果是正常的,这是为啥呢??????????
0 0
- Add Binary —LeetCode
- Leetcode——67. Add Binary
- LeetCode——Add Binary
- LeetCode——Add Binary
- [LeetCode]67.Add Binary
- LeetCode --- 67. Add Binary
- [Leetcode] 67. Add Binary
- [leetcode] 67.Add Binary
- 【leetcode】67. Add Binary
- [leetcode] 67.Add Binary
- [leetcode] 67. Add Binary
- 【Leetcode】67. Add Binary
- 67. Add Binary LeetCode
- 【LeetCode】67. Add Binary
- leetcode--67. Add Binary
- leetcode 67. Add Binary
- LeetCode 67. Add Binary
- LeetCode *** 67. Add Binary
- HDU5546 Ancient Go(DFS)
- Matlab坐标轴操作汇总
- jQuery中设置form表单中action值的方法
- 新年简单谈谈java异常
- 【Linux学习】之 磁盘及文件系统管理
- Leetcode——67. Add Binary
- VS设置了断点竟然不起作用
- Sublime Text3常用使用技巧
- POJ3191-The Moronic Cowmpouter
- Java-IO之BufferedInputStream(缓冲输入流)
- nginx配置https双向认证
- 内部类
- javascript 基础 幻灯片图片
- Poj 2524 Ubiquitous Religions (并查集