Add Binary

本题收获:

对于相加而言,考虑进位以及进位之后的值为多少,全部进位完毕后进位还为1(11 + 11 = 110)需要添加一位.
1.string中默认每个元素为char型

2.从int型转化为char型  s[i] - '0'

 从char型转化为int型  s[i] + '0'

3.char型和int型相加时按上式会报错   s = (char)(s[i] + '0') +s

  s = (char)(s[i] + '0') +s 这样的写法 每次新的s(由(char)(s[i] + '0'))会放在左边,(加的s为老的s)

  s += (char)(s[i] + '0') 每次新的s会放在右边

  注意高低位来决定“+”的位置

4.c += i >= 0 ? a[i] - '0' : 0;这个语句的执行是    c += (i >= 0 ? a[i] - '0' : 0); 首先执行判断语句 ? :,在执行表达式c +=>

5.stray '\343' in program leetcode中报错,将报错行的空格全部删除,需要在重新打上(在写代码时可能出现全角符号的值或者是空格,好好检查下,或者重新输入)

 题目:

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

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

    我的思路:每位相加,但是没想出具体怎么做,其实这样行不通

    leetcode:利用中间int型数,每次加两个数的相同位,然后在转化为string型,产生的进位保留加到下次循环。

  代码:思路非常棒

  1. class MyClass
  2. {
  3. public:
  4. string addBinary(string a, string b)
  5. {
  6. string s;
  7. long c = ;
  8. int i = a.size() - , j = b.size() - ;
  9.  
  10. while (i >= || j >= || c ==) //注意循环条件 || c == 1
  11. {
  12. c += i >= ? a[i] - '' : ; //string默认的为char型
  13. i--;
  14. c += j >= ? b[j] - '' : ; //从char型到int型 -'0'
  15. j--;
  16. s = (char)((c % ) + '') +s ; //从int型到char型 +'0'
  17. c = c / ;                //注意什么时候% ,什么时候 /
  18. }
  19.  
  20. return s;
  21. }
  22. };

  我的测试代码:

  1. // Add Binary.cpp : 定义控制台应用程序的入口点。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "iostream"
  6. #include "string"
  7. using namespace std;
  8.  
  9. class MyClass
  10. {
  11. public:
  12. string addBinary(string a, string b)
  13. {
  14. string s;
  15. long c = ;
  16. int i = a.size() - , j = b.size() - ;
  17.  
  18. while (i >= || j >= || c ==) //注意循环条件
  19. {
  20. c += i >= ? a[i] - '' : ; //string默认的为char型
  21. i--;
  22. c += j >= ? b[j] - '' : ; //从char型到int型 -'0'
  23. j--;
  24. s = (char)((c % ) + '') +s ; //从int型到char型 +'0'
  25. c = c / ;
  26. }
  27.  
  28. return s;
  29. }
  30. };
  31.  
  32. int _tmain(int argc, _TCHAR* argv[])
  33. {
  34. string a, b, res;
  35. cin >> a;
  36. cin >> b;
  37. MyClass solution;
  38. res = solution.addBinary(a, b);
  39. cout << res << endl;
  40. system("pause");
  41. return ;
  42. }

2016.6.21——Add Binary的更多相关文章

  1. FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM

    FFMpeg ver 20160219-git-98a0053 滤镜中英文对照 2016.02.21 by 1CM T.. = Timeline support 支持时间轴 .S. = Slice t ...

  2. Technical Committee Weekly Meeting 2016.06.21

    Meeting time: 2016.June.21 1:00~2:00 Chairperson:  Thierry Carrez Meeting summary: 1.Add current hou ...

  3. Murano Weekly Meeting 2016.06.21

    Meeting time: 2016.June.21 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: 1. ...

  4. leetcode解题:Add binary问题

    顺便把之前做过的一个简单难度的题也贴上来吧 67. Add Binary Given two binary strings, return their sum (also a binary strin ...

  5. [LintCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). Have you met this question in a r ...

  6. Add Binary

    Add Binary https://leetcode.com/problems/add-binary/ Given two binary strings, return their sum (als ...

  7. ”耐撕“团队 2016.3.21 站立会议3 2 1 GO!

    ”耐撕“团队 2016.3.21 站立会议 时间:2016.3.21  ① 17:20-17:45  ②17:55-18:10  总计40分钟 成员: Z 郑蕊 * 组长 (博客:http://www ...

  8. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

  9. 67. Add Binary【LeetCode】

    67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = &q ...

随机推荐

  1. Cannot create file"C:\Users\LML\AppData\Local\Temp\EditorLineEnds.ttr"。另一个程序正在使用此文件,进程无法访问。

    不能二次启动,每次开机第一次都ok,出于习惯,总是想试试第二次打开软件是否正常,结果不出所料,出现了“Cannot create file"C:\Users\LML\AppData\Loca ...

  2. 是否升级IOS11?IOS11不支持32位程序 查看手机哪些APP不支持

    查看苹果32位APP具体步骤:设置-通用-关于本机-应用程序.如果手机中下载了32位应用的话,苹果会给出应用兼容性提醒:如果手机里没有安装32位应用,右侧没有小三角,点击“应用程序”也会没有反应. I ...

  3. 【转载】JSP生成静态Html页面

    在网站项目中,为了访问速度加快,为了方便百度爬虫抓取网页的内容,需要把jsp的动态页面转为html静态页面.通常有2种常用的方式: 1.伪静态,使用URL Rewriter 2.纯静态,本文中代码实现 ...

  4. ADO.NET:C#/SQL Server

    1.首次要准备的(工具)是:a.Microsoft Visual Studio Ultimate 2012;b.Microsoft SQL Server Management Studio ; 2.首 ...

  5. installns

    installns 将升级文件NSVPX-NCore_build-12.1-48.13_nc_64.tgz,上传至设备的“/var/nsinstall”目录下. 在命令行中执行以下命令,查看升级脚本使 ...

  6. 洛谷P5283 & LOJ3048:[十二省联考2019]异或粽子——题解

    https://www.luogu.org/problemnew/show/P5283 https://loj.ac/problem/3048 小粽是一个喜欢吃粽子的好孩子.今天她在家里自己做起了粽子 ...

  7. WEB入门.九 导航菜单

    学习内容 水平导航菜单 垂直导航菜单 下拉式导航菜单 能力目标 制作tab标签导航菜单 制作带箭头的导航菜单 制作带信息提示的导航菜单 制作垂直下拉导航菜单 制作水平下拉导航菜单 本章简介 上一章节中 ...

  8. 我的第一个activiti实例 (代码方式) ctiviti入门列子一个简单的activiti请假流程

    转: (activiti入门列子一个简单的activiti请假流程) 我的第一个activiti实例 2017年05月31日 14:29:45 chf_mixueer 阅读数:1223   整个项目的 ...

  9. laravel Collection mapToDictionary 例子

    源码 示例 <?php require __DIR__ . '/bootstrap/app.php'; $arr = [ [ 'name' => 'John', 'age' => 2 ...

  10. supervisor 添加新配置不生效的问题

    supervisorctl reread supervisorctl reload (不运行这一步会导致启动不了) supervisorctl start xxx:* 提示:No config upd ...