【leetcode】Add Binary
来源:互联网 发布:金克丝神龙烈焰淘宝 编辑:程序博客网 时间:2024/05/17 08:08
class Solution {public: string addBinary(string a, string b) { // Note: The Solution object is instantiated only once and is reused by each test case. reverse(a.begin(),a.end()); reverse(b.begin(),b.end()); string ans=""; int carry=0; int len=max(a.size(),b.size()); for(int i=0;i<len;i++) { int ca=0; if(i<a.size()) ca=a[i]-'0'; int cb=0; if(i<b.size()) cb=b[i]-'0'; int sum=ca+cb+carry; ans+=sum%2+'0';//注意此处是+=,而不是ans[i]= carry=sum/2; } if(carry==1) ans+='1'; reverse(ans.begin(),ans.end()); return ans; }};
- 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
- iOS7 uilabel 自动换行,文字自适应
- 快速搭建Android NDK环境
- 应用360云盘与SVN版本管理服务器搭建基于云端的版本控制软件
- cout << (a += a -= a*a)和cout << a += a -= a*a
- VisualStudio开发工具中的C#主流控件属性及事件汇总(菜鸟必备)
- 【leetcode】Add Binary
- HTK安装和测试
- (11)UDP:用户数据报协议
- (12)广播和多播
- (13)IGMP:Internet组管理协议
- (14)DNS:域名系统
- (15)TFTP:简单文件传送协议
- (16)BOOTP:引导程序协议
- (17)TCP:传输控制协议