LeetCode Add Binary
来源:互联网 发布:ubuntu华屏 编辑:程序博客网 时间:2024/06/08 13:29
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) { stack<int> s; int na = a.size() - 1; int nb = b.size() - 1; int c = 0; while (na > -1 || nb > -1) { int va = 0; if (na > -1) { va = a[na] - '0'; na--; } int vb = 0; if (nb > -1) { vb = b[nb] - '0'; nb--; } int tmp = va + vb + c; c = tmp / 2; s.push(tmp % 2); } if (c != 0) s.push(c); string ans; while (!s.empty()) { ans.push_back(s.top()+'0'); s.pop(); } return ans; }};
0 0
- 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
- 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:Add Binary
- 如何把txt文档导入到plist文件中
- 我的第一次
- POJ 3292 Semi-prime H-numbers (变形埃氏筛法)
- 临时存放的东东
- Flex 通过反射清空form表单中的内容
- LeetCode Add Binary
- Win64平台CUDA6.5+VS2010+opencv2.4.8配置
- NetworkComms V3 上传文件探讨
- Merge Two Sorted Lists(LeetCode)
- div+css学习笔记(十天学会div+css)
- 控制器的生命周期
- Android Please ensure that adb is correctly located at问题解决
- Python中的打开文件对话框(转)
- 第三周项目一(2)