题目

代码

class Solution {
public:
string addBinary(string a, string b)
{
int lenA = a.length();
int lenB = b.length();
string result;
int addFlag = 0;
int i;
int current = 0;
for (i = 0;i < lenA&&i < lenB;i++)
{
int numA = a[lenA - i - 1] - '0', numB = b[lenB - i - 1] - '0';
current = (addFlag + numA + numB) % 2;
result.push_back((char)('0' + current));
addFlag = numA + numB + addFlag > 1 ? 1 : 0; } if (lenA > lenB)
{
for (;i < lenA;i++)
{
current = ((a[lenA - i - 1] - '0') + addFlag) % 2;
result.push_back('0' + current);
addFlag = a[lenA - i - 1]-'0' + addFlag > 1 ? 1 : 0;
}
if (addFlag == 1)
result.push_back('1');
}
else if (lenA < lenB)
{
for (;i < lenB;i++)
{
current = ((b[lenB - i - 1] - '0') + addFlag) % 2;
result.push_back('0' + current);
addFlag = b[lenB - i - 1]-'0' + addFlag > 1 ? 1 : 0;
}
if (addFlag == 1)
result.push_back('1');
}
else
{
if (addFlag == 1)
result.push_back('1');
}
std::reverse(result.begin(), result.end());
return result;
}
};

最新文章

  1. [LeetCode] Spiral Matrix 螺旋矩阵
  2. 【转】Struts1.x系列教程(4):标签库概述与安装
  3. Linux 删除文件夹和文件命令
  4. GNU make 规则
  5. myeclipse配置下tomcat debug启动很无比慢
  6. 解决浏览器background-image属性不支持css3动画
  7. shell入门之函数应用 分类: 学习笔记 linux ubuntu 2015-07-10 21:48 77人阅读 评论(0) 收藏
  8. Android TextView 手动上下滑动
  9. dp状态压缩-铺砖问题
  10. spring注解支持
  11. P2P直连?经服务器中转?
  12. iOS.Animations.by.Tutorials.v2.0汉化
  13. JAVA面向对象-----抽象类注意细节
  14. python自动化开发-[第十四天]-javascript(续)
  15. Kibana中的Coordinate Map地图报索引错误的问题
  16. JQuery实用技巧
  17. Java语言的主要特点
  18. Python输出信息
  19. poj1284 Primitive Roots
  20. CSS学习笔记(9)--详解CSS中:nth-child的用法

热门文章

  1. 使用工厂方法模式设计能够实现包含加法(+)、减法(-)、乘法(*)、除法(/)四种运算的计算机程序,要求输入两个数和运算符,得到运算结果。要求使用相关的工具绘制UML类图并严格按照类图的设计编写程序实
  2. 齐博x1如何录制阿里等第三方直播流
  3. Python学习三天计划-3
  4. Sentinel 介绍与下载使用
  5. ssh登录提示hosts is down
  6. python中展示json数据不换行(手动换行)
  7. 图文详解在VMware Workstation 16 PRO虚拟机上安装Rocky 8.6 linux系统
  8. VS2019 iis无法在Web服务器上启动调试。打开的URL的IIS辅助进程当前没有运行
  9. 关于python实现html转word(docx)
  10. 《HelloGitHub》第 80 期