[LeetCode] 67. Add Binary
来源:互联网 发布:软件开发流程文档 编辑:程序博客网 时间:2024/05/21 07:51
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) { if (a.size() < b.size()) return addBinary(b,a); b.insert(0, a.size() - b.size(), '0'); string res; int carry = 0; for (int i = (int)a.size() - 1; i >= 0; i--) { int sum = (a[i] - '0') + (b[i] - '0') + carry; carry = sum / 2; sum %= 2; res.insert(0, 1, sum + '0'); } if (carry) res.insert(0, 1, '1'); return res; }};
阅读全文
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
- ZOJ 3761 —— Easy billiards(并查集+深搜)
- CopyOnWriteArrayList
- linux 动态库的显示调用
- php禁止复制网页内容功能
- 比赛时需要注意的几个地方
- [LeetCode] 67. Add Binary
- LintCode5
- iOS 如何获取设备的各种信息
- supervisor ERROR (spawn error)
- android 6.0 logcat机制(三)logd处理请求log
- win10运行程序提示“为了对电脑进行保护,已经阻止此应用” 解决方法
- Django 学习笔记之 Class-Based-View
- C#之app.config、exe.config和vshost.exe.config作用区别
- RTandroid ADB MODE: Introduction to accessing the Raspberry Pi's GPIO in C++ (sysfs)