【leetcode】67. Add Binary
来源:互联网 发布:域名攻击 编辑:程序博客网 时间:2024/04/29 12:41
一、题目描述
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
思路:注意进位的情况。原理都懂,但是简洁的代码不好写。这里参考了别人的代码。
c++代码(10ms,6.61%)
class Solution {public: string addBinary(string a, string b) { string result=""; int i=a.size()-1; int j=b.size()-1; int c=0; while(i>=0 || j>=0 || c==1){ c+= i>=0?a[i--]-'0':0; c+= j>=0?b[j--]-'0':0; result = char(c%2+'0') + result; c/=2; }//while return result; }};
0 0
- [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
- leetcode 67. Add Binary
- 【leetcode】67.Add Binary
- LeetCode 67. Add Binary
- LeetCode-67.Add Binary
- 猿团专访 |以技术推动发展 msup 成为企业经验智库
- 自定义倒计时控件
- C语言基础--静态存储和动态存储
- js二级下拉菜单的关联
- mysql 复合索引 总结
- 【leetcode】67. Add Binary
- 下拉刷新的逻辑要点
- 深入Scala系列之一组件重用
- 2016.06.30--月总结
- 有关矩阵低秩近似的一个不等式
- 树莓派----02wifi 热点
- Grunt安装教程详解
- 静态工厂(简单工厂)模式
- 策略模式(Strategy Pattern)