[Leetcode]Add Binary
来源:互联网 发布:vs2010读取excel数据 编辑:程序博客网 时间:2024/06/06 17:04
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
class Solution {public: /*algorithm add it from the tail, plug carry time O(max(m,n)) space O(max(m,n) + 1) */ string addBinary(string a, string b) { int m = a.size(),n = b.size(); string s; int carry = 0,sum; char c; while(m > 0 || n > 0){ sum = carry; if(m > 0)sum += a[--m] - '0'; if(n > 0)sum += b[--n] -'0'; c = sum%2 + '0'; carry = sum/2; s.insert(0,1,c); } if(carry > 0)s.insert(0,1,'1'); return s; }};
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
- 安装Ubuntu server 时报错 "The 'grub-pc' package failed to install into /target/ 的解决方法
- 使用MetaSploit在WinXP对Win2000进行攻击
- 自定义iOS UIpagecontrol 小圆点 换上自定义图片
- 静下心来谈谈平衡二叉树
- python中单继承和多继承中子类默认继承父类的哪个构造函数__init__
- [Leetcode]Add Binary
- SQL Server 2008下日志清理方法
- ButterKnife官方介绍
- NEU 1639: Small problem
- [笔记][Java7并发编程实战手册]2.5使用Lock实现同步二
- Codeforces Round #315 (Div. 2) B. Inventory (水题)
- hdu 3501
- Blog转移(暂时/永久)
- Java反射浅谈