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型,产生的进位保留加到下次循环。

  代码:思路非常棒

 class MyClass
{
public:
string addBinary(string a, string b)
{
string s;
long c = ;
int i = a.size() - , j = b.size() - ; while (i >= || j >= || c ==) //注意循环条件 || c == 1
{
c += i >= ? a[i] - '' : ; //string默认的为char型
i--;
c += j >= ? b[j] - '' : ; //从char型到int型 -'0'
j--;
s = (char)((c % ) + '') +s ; //从int型到char型 +'0'
c = c / ;                //注意什么时候% ,什么时候 /
} return s;
}
};

  我的测试代码:

 // Add Binary.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include "iostream"
#include "string"
using namespace std; class MyClass
{
public:
string addBinary(string a, string b)
{
string s;
long c = ;
int i = a.size() - , j = b.size() - ; while (i >= || j >= || c ==) //注意循环条件
{
c += i >= ? a[i] - '' : ; //string默认的为char型
i--;
c += j >= ? b[j] - '' : ; //从char型到int型 -'0'
j--;
s = (char)((c % ) + '') +s ; //从int型到char型 +'0'
c = c / ;
} return s;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
string a, b, res;
cin >> a;
cin >> b;
MyClass solution;
res = solution.addBinary(a, b);
cout << res << endl;
system("pause");
return ;
}

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. Java多线程(一) —— 传统线程技术

    一.传统线程机制 1. 使用类Thread实现 new Thread(){ @Override public void run() { while(true){ try{ Thread.sleep(2 ...

  2. 【Python】Python文件系统功能:os模块

    1.os模块方法分类 (1)目录: chdir() 改变工作目录 chroot() 设定当前进程的根目录 listdir() 列出指定目录下的所有文件名 mkdir() 创建指定目录 makedirs ...

  3. BZOJ5125 小Q的书架(决策单调性+动态规划+分治+树状数组)

    设f[i][j]为前i个划成j段的最小代价,枚举上个划分点转移.容易想到这个dp有决策单调性,感性证明一下比较显然.如果用单调栈维护决策就不太能快速的求出逆序对个数了,改为使用分治,移动端点时树状数组 ...

  4. 手写简单的promise

    function Promise(fn) { var that = this; this.status = "pedding"; this.value = undefined; / ...

  5. Day24-Ajax文件上传

    一. <input type="file" id="fafafa" name="afafaf"/> <input type ...

  6. poj2761 feed the dog

    题目链接:http://poj.org/problem?id=2761 Description Wind loves pretty dogs very much, and she has n pet ...

  7. 20135239 益西拉姆 linux内核分析 进程的切换和系统的一般执行过程

    week 8 进程的切换和系统的一般执行过程 [ 20135239 原文请转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course ...

  8. Java考试题之五

    QUESTION 102 Given: 23. Object [] myObjects = { 24. new Integer(12), 25. new String("foo") ...

  9. Android Studio下“Error:Could not find com.android.tools.build:gradle:2.2.1”的解决方法

    ref from: Android Studio下“Error:Could not find com.android.tools.build:gradle:2.2.1”的解决方法http://blog ...

  10. [POI2008] BLO

    link 试题分析 分两种情况考虑. 当此点不是割点是,答案是$2\times (n-1)$. 当是割点时,我们发现这个点把树分成了若干个联通块,只要两两相乘即可. #include<iostr ...