[leetcode刷题系列]Add Binary
2013-08-06 16:59
274 查看
- - 模拟题咯
class Solution { public: string addBinary(string a, string b) { // Start typing your C/C++ solution below // DO NOT write int main() function reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); string ans = ""; int carry = 0; int len = max(a.size(), b.size()); for(int i = 0; i < len; ++ i){ int ca = 0; if(i < a.size()) ca = a[i] - '0'; int cb = 0; if(i < b.size()) cb = b[i] - '0'; int sum = ca + cb + carry; carry = sum / 2; ans += sum % 2 + '0'; } if(carry) ans += '1'; reverse(ans.begin(), ans.end()); return ans; } };
相关文章推荐
- leetCode(52):Add Binary 分类: leetCode 2015-07-28 23:03 41人阅读 评论(0) 收藏
- LeetCode刷题系列_43题
- leetcode系列(3)--Remove Duplicates from Sorted List II
- 【LeetCode系列】动态规划算法
- “全排列”问题系列(一)[LeetCode] - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题
- leetcode:Add Binary
- <leetcode系列> String to Integer (atoi) 以及atoi源码实现
- 【Leetcode系列之二】Leetcode OJ 11-20
- [LeetCode] Add Binary
- leetcode系列(三):Intersection of two arrays
- LeetCode - Add Binary
- 算法练习专题——LeetCode系列之 ThreeSum
- Leetcode 题解系列(十一)
- [Leetcode] Add Binary
- LeetCode——single-number系列
- 【Leetcode】Stock Buy and Sell 系列问题 终极分析!
- LeetCode(67)题解: Add Binary
- LeetCode题解系列--712. Minimum ASCII Delete Sum for Two Strings
- LeetCode 18 4Sum K-sum系列
- LeetCode题解:Add Binary