LeetCode OJ Add Binary
来源:互联网 发布:人工智能包括机器人吗 编辑:程序博客网 时间:2024/06/04 17:47
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) { reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); if (a.size() < b.size()) swap(a, b); b.append(a.size() - b.size(), '0'); string ans(a.size() + 2, '0'); for (int i = 0; i < a.size(); i++) { ans[i + 1] += ((ans[i] - '0') + (a[i] - '0') + (b[i] - '0')) / 2; ans[i] = ((ans[i] - '0') + (a[i] - '0') + (b[i] - '0')) % 2 + '0'; } int i; for (i = ans.size() - 1; i > 0 && ans[i] == '0'; i--); ans = ans.substr(0, i + 1); reverse(ans.begin(), ans.end()); return ans; }};
0 0
- LeetCode OJ:Add Binary
- LeetCode OJ - Add Binary
- LeetCode OJ Add Binary
- <LeetCode OJ> 67. Add Binary
- LeetCode Oj 67. Add Binary
- Leetcode OJ 67 Add Binary [Easy]
- LeetCode OJ算法题(六十五):Add Binary
- LeetCode OJ 之 Add Binary (二进制数字相加)
- [leedcode oj 67]Add Binary
- LeetCode: Add Binary
- [Leetcode] Add Binary
- LeetCode Add Binary
- Leetcode: Add Binary
- [Leetcode] Add Binary
- LeetCode : Add Binary
- LeetCode Add Binary
- [LeetCode] Add Binary
- leetcode 58: Add Binary
- IntentService的使用
- 自学Java的路上
- struts2
- LeetCode OJ Spiral Matrix
- To R小米
- LeetCode OJ Add Binary
- Mysql 免安装版 配置
- JS C++
- LeetCode OJ Set Matrix Zeroes
- ID3DXAnimationController动画控制器接口初步了解
- Eclipse提示内存不足
- 第一章《人造与天生》
- 三级网络备考——自信
- LeetCode OJ Merge Intervals