2016.6.21——Add Binary

Add Binary

本题收获:
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会放在右边

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

  题目:

  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 = 0;
 8         int i = a.size() - 1, j = b.size() - 1;
 9
10         while (i >= 0 || j >= 0 || c ==1)        //注意循环条件 || c == 1
11         {
12             c += i >= 0 ? a[i] - ‘0‘ : 0;        //string默认的为char型
13             i--;
14             c += j >= 0 ? b[j] - ‘0‘ : 0;        //从char型到int型 -‘0‘
15             j--;
16             s = (char)((c % 2 ) + ‘0‘) +s ;        //从int型到char型 +‘0‘
17             c = c / 2;                //注意什么时候% ,什么时候 /
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 = 0;
16         int i = a.size() - 1, j = b.size() - 1;
17
18         while (i >= 0 || j >= 0 || c ==1)        //注意循环条件
19         {
20             c += i >= 0 ? a[i] - ‘0‘ : 0;        //string默认的为char型
21             i--;
22             c += j >= 0 ? b[j] - ‘0‘ : 0;        //从char型到int型 -‘0‘
23             j--;
24             s = (char)((c % 2 ) + ‘0‘) +s ;        //从int型到char型 +‘0‘
25             c = c / 2;
26         }
27
28         return s;
29     }
30 };
31
32
33
34
35 int _tmain(int argc, _TCHAR* argv[])
36 {
37     string a, b, res;
38     cin >> a;
39     cin >> b;
40     MyClass solution;
41     res = solution.addBinary(a, b);
42     cout << res << endl;
43     system("pause");
44     return 0;
45 }
时间: 2024-08-30 01:00:20

2016.6.21——Add Binary的相关文章

LeetCode: Add Binary 解题报告

Add BinaryGiven two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". SOLUTION: 指针指到两个字符串的末尾,不断往前推进,用carry表示进位.用stringbuilder来记录结果. 使用insert(0, c)函数将加出的结果不断插入到STRINGBUILDER. 1 p

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 threading 分段线程 ..C = Command support 支持命令传送 A = Audio input/output 音频 输入/输出 V = Video input/output 视频 输入/输出 N = Dynamic number and/or type of input/out

67. Add Binary【LeetCode】

67. Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 1 public class Solution { 2 public String addBinary(String a, String b) { 3 String res ="";

[LeetCode][JavaScript]Add Binary

https://leetcode.com/problems/add-binary/ Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 长整型二进制加法. 1 /** 2 * @param {string} a 3 * @param {string} b 4 *

Add Binary Leetcode java

题目: Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 题解: 二进制加法都是从最低位(从右加到左).所以对两个字符串要从最后一位开始加,如果遇见长度不一的情况,就把短的字符串高位补0. 每轮计算要加上进位,最后跳出循环后要坚持进位是否为1,以便更新结果. 代码如下(from

[LeetCode] [Add Binary 2012-04-02 ]

Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". string 的操作,短string补位.两个"0"会输出一个"00",要特殊处理,plus如果最后为"1",要补上. ? 1 2 3 4 5 6 7 8 9 10 1

No.67 Add Binary

No.67 Add Binary Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 与No.2 Add Two Numbers的思路非常相似,不同在:对其进行运算时,其实是要逆向相加的!!!因为边界控制的原因,可先将字符串逆序reverse( ). 1 #include "

Technical Committee Weekly Meeting 2016.06.21

Meeting time: 2016.June.21 1:00~2:00 Chairperson:  Thierry Carrez Meeting summary: 1.Add current house rules for reference This lists exceptions to the formal votes for various changes in the openstack/governance repository. It corresponds to house r

【leetcode刷题笔记】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 题解:简单的二进制加法模拟.a,b的最后以为对齐开始进行加法,用carries保存进位,如果加完后最高位还有进位,那么要在结果的最前面加一个1. 代码如下: 1 public class Solution { 2 public Str