Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

Hide Tags

Math String

class Solution {
public:
string addBinary(string a, string b) {
string longer=a.length()>b.length() ? a:b;
string shorter=a.length()>b.length() ? b:a;
int add=;
int j=longer.length()-;
for(int i=shorter.length()-;i>=;--i,--j){
if(shorter[i]-''+longer[j]-''+add>){
longer[j]=''+(shorter[i]-''+longer[j]-''+add)%;
add=;
}else{
longer[j]=''+(shorter[i]-''+longer[j]-''+add)%;
add=;
}
}
while(j>=){
if(longer[j]-''+add>){
longer[j]='';
add=;
}else{
longer[j]=longer[j]+add;
add=;
}
--j;
}
if(add)
longer.insert(,,'');
return longer;
}
};

最新文章

  1. android UI控件小记
  2. 十一、Manipulators
  3. Replication的犄角旮旯(六)-- 一个DDL引发的血案(上)(如何近似估算DDL操作进度)
  4. 【Swift学习】Swift编程之旅---集合类型之数组(六)
  5. specify a file path to store the seed
  6. 《Play for Java》学习笔记(四)Controller
  7. 如何让你的Apache支持include文件解析和支持shtml的相关配置
  8. IIS 7.0 and Web Farms
  9. web前端开发分享-css,js入门篇(转)
  10. imacros实现Excel数据自动录入到网页中
  11. GNU名称解析
  12. 推荐几款主流的Css Reset
  13. 六:在线工具网站,让你PC上要装的软件少一半!
  14. Python项目部署-使用Nginx部署Django项目
  15. Stars HDU - 1541
  16. VCS
  17. c#读sql server数据添加到MySQL数据库
  18. [日常工作]Oracle新增数据文件的小知识点
  19. Python的二叉树实现
  20. [bzoj 4034][HAOI 2015]树上操作

热门文章

  1. MySQL数据库 字段操作 多表关系(更新中...)
  2. Elasticsearch快速开始
  3. GULP入门(一)
  4. PLSQLDeveloper链接报错 解决办法
  5. Chrome 地址栏如何设置显示 http/https 和 www
  6. JAVA面试常见问题之基础篇
  7. 如何在 Flink 1.9 中使用 Hive?
  8. 使用jquery封装一个可以复用的提示框
  9. linux查看现在在运行的进程 $ pstree -a$ ps aux
  10. select2 如何自定义提示信息-布布扣-bubuko.com