题目:

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

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

代码:

class Solution {
public:
string addBinary(string a, string b) {
std::string result;
std::string::reverse_iterator ia = a.rbegin();
std::string::reverse_iterator ib = b.rbegin();
int carry = ;
for ( int sum=, part_a=, part_b=; ia!=a.rend() || ib!=b.rend(); ia==a.rend()? a.rend() : ++ia, ib==b.rend()? b.rend() : ++ib )
{
part_a = ia==a.rend() ? : *ia-'';
part_b = ib==b.rend() ? : *ib-'';
sum = part_a + part_b + carry;
result.insert(result.begin(), sum%+'');
carry = sum/;
}
if ( carry== ) result.insert(result.begin(), '');
return result;
}
};

tips:

1. 熟悉string的一些方法:rbegin rend insert

2. 熟悉int与其对应的char的转换方法。

顺道复习了一下c++ primer plus上类型转换的章节:

1. 赋值时进行转换: 变量类型不一样,可能产生潜在的丢失或者失去精度。

2. 表达式中的转换

3. 传参时类型转换

4. 强制类型转换

另外,

part_a = ia==a.rend() ? 0 : *ia-'0'; (int)

result.insert(result.begin(), sum%2+'0'); (char)

同样是int与char之间的加减运算,为什么返回的类型不一样呢?

猜测应该是这样的:运算时都转化成ASCII码的值进行加减;最后如果要求是int的就保持int型,需要是char的就转换成char型。

c++ primer plus (第四版) 45~47讲解了这种转换。

'0'可以理解为字符常量,在内存中就是按照整型存的。

又试验了一下

char c = 65+1;

int i = 65+1;

cout << c << endl;

cout << i << endl;

输出结果是:

B

66

通过这个例子就可以记住char的一些性质了

另外,通过这个例子,复习了单双引号的不同:c++中char常量用单引号括起;双引号表示字符串,二者差别还是很大的。

PS:之前还傻搜字符跟整形转换... c++中将char在内存中存成了整型,正是因此带来了极大的便利,可以很轻松的通过加减来灵活处理字符。

那么,如果是中文或者日文这种不止256个字符的语系呢?可以有wchar_t类型(本质就用来编码的bits比8位多了)

wchar_t a = L'A' (前缀L表示宽字符常量)

========================================

第二次过这道题,复习了一下digit跟char的一些操作运算。

class Solution {
public:
string addBinary(string a, string b) {
const int len_a = a.size();
const int len_b = b.size();
vector<char> ret;
int i_a = len_a-;
int i_b = len_b-;
int carry = ;
while ( i_a>= || i_b>= )
{
int part1 = i_a>= ? a[i_a] : '';
int part2 = i_b>= ? b[i_b] : '';
int tmp_sum = (part1-'') + (part2-'') + carry;
carry = tmp_sum / ;
int curr = tmp_sum % ;
ret.insert(ret.begin(), curr+'');
i_a = i_a>= ? i_a- : i_a;
i_b = i_b>= ? i_b- : i_b;
}
if (carry==) ret.insert(ret.begin(), carry+'');
return string(ret.begin(),ret.end());
}
};

【Add binary】cpp的更多相关文章

  1. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  2. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

  3. 【Same Tree】cpp

    题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

  4. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  5. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  6. 【Text Justification】cpp

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

  7. 【Divided Two】cpp

    题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...

  8. 【Multiply Strings】cpp

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

  9. 【Minimum Window】cpp

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

随机推荐

  1. HTML 5缓存机制:Cache Manifest配置实例

    Cache Manifest是HTML 5的一种缓存机制,文章作者直接用博客当测试环境,虽然[color=#444444 !important]应用起来非常简单,但效果却出奇的好.缓存后的速度,简直是 ...

  2. 华为OJ-合唱队

    华为OJ-初级题-合唱队 思路与分析 本题可以用DP的方法,分别从正向和逆向的两个方向求,该数组即186 186 150 200 160 130 197 200的上升对大序列.正向为[1, 1, 1, ...

  3. asp.net mvc 用Redis实现分布式集群共享Session。

    1.这两天研究Redis搞分布式session问题,网上找的资料都是用ServiceStack.Redis来实现的,但是在做性能测试的时候发现最新的v4版本有限制每小时候最多请求6000次,因为官网开 ...

  4. Lucene全文检索框架

    1.什么时Lucene? 是一个全文搜索框架,而不是应用产品,他只是一种工具让你能实现某些产品,并不像www.baidu.com拿来就能用 是apache组织的一个用java实现的全文搜索引擎的开源项 ...

  5. LotusPhp中配置文件组件LtConfig详解

    LotusPhp中配置文件组件LtConfig是约定的一个重要组成部分,适用于多个场景,多数的LotusPhp组件如数据库,缓存,RBAC,表单验证等都需要用到配置组件,LtConfig配置组件也是L ...

  6. php中的日期

    1.在PHP中获取日期和时间 time()返回当前时间的 Unix 时间戳. getDate()返回日期/时间信息. gettimeofday()返回当前时间信息.date_sunrise()返回给定 ...

  7. C# 多线程 简单使用方法以及常用参数

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. [转]PROC简单使用用例--VC连接ORACLE

    [转]PROC简单使用用例--VC连接ORACLE 操作系统:windows 7 数据库版本:oracle 10g VS版本:VS2010 前言:连接ORACLE的方式有很多,此处仅以PROC为例,说 ...

  9. AppCan移动平台开发常见问题解答

    在使用AppCan移动平台开发跨平台APP时,有开发者会遇到一些问题, 不急,跟笔者一起来聊一聊使用AppCan平台开发中常见问题的解答方法. 问1.正常是按照官网提供的4个iphone启动图尺寸来做 ...

  10. ListView的多布局中的小问题

    今天用到了ListView的多布局,我们需要额外重写两个方法 //返回多布局的个数 @Override public int getViewTypeCount() { return 3; } //用该 ...