[LeetCode]67. Add Binary
来源:互联网 发布:java.swing包 编辑:程序博客网 时间:2024/05/16 17:42
[LeetCode]67. Add Binary
题目描述
思路
字符串后往前遍历,记得处理进位即可
代码
#include <iostream>#include <string>#include <algorithm>using namespace std;class Solution {public: string addBinary(string a, string b) { string res; int carry = 0; int aLen = a.size(), bLen = b.size(), len = max(a.size(), b.size()); for (int i = 1; i <= len; ++i) { int addA = 0, addB = 0; if (i <= aLen) addA = a[aLen - i] - '0'; if (i <= bLen) addB = b[bLen - i] - '0'; int sum = (addA + addB + carry) % 2; carry = (addA + addB + carry) / 2; res = to_string(sum) + res; } if (carry) res = to_string(carry) + res; return res; }};int main() { Solution s; cout << s.addBinary("10", "1") << endl; system("pause"); return 0;}
阅读全文
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
- 单步调试(F11)时会自动跳到ostream文件中解决方法
- leetcode 将已排序的 数组/链表 转换为二叉搜索树(BST),Python实现
- 微信小程序-思维脑图
- 1004. 成绩排名 (20) PAT(B级)
- Kotlin基础教程-HelloWorld
- [LeetCode]67. Add Binary
- JavaScript特点
- 1.Javabean属性
- HashSet,TreeSet,HashMap,一对一,一对多
- 加载外部OBJ模型-03
- B
- Radar Installation
- 功能强大的文件上传插件带上传进度-WebUploader
- Vue.js(过滤器)