题目:

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. ado.net工厂模式DbProviderFactories

    DbProviderFactory f = DbProviderFactories.GetFactory(System.Configuration.ConfigurationManager.Conne ...

  2. 消息推送之APNS

    利用APNS进行消息推送 原理 APNS 是Apple Push Notification Service(Apple Push服务器)的缩写,是苹果的服务器. APNS推送可以分为三个阶段: 第一阶 ...

  3. 【转】Linux模式设计5-位图操作

    通过位图提供的两种状态可以在非常节约内存的情况下表示开关变量,并且同类这类变量可以紧凑而高效的统一进行处理.有很多内核子系统都需要位图的支持,但是不同的情况又需要不同的位图个数,比如SMP系统上的CP ...

  4. Android Virtual Devices代理上网

    本机电脑是使用代理上网,然后要在avd中要连接互联网,设置步骤如下: Click on Menu Click on Settings Click on Wireless & Networks ...

  5. Linux shell实现Mysql异地备份数据库

    #--------------------------Mysqldump异地备份-----------------# #!/bin/bash #start mysqldump back /usr/bi ...

  6. Curses library not found. Please install appropriate package

    今天安装mysql-5.5.3-m3的时候,报下面的错误: -- Could NOT find OpenSSL (missing: OPENSSL_LIBRARIES OPENSSL_INCLUDE_ ...

  7. js dom 操作

    JS的DOM操作   1DOM是文档对象模型,这种模型为树模型:文档是指标签文档,对象是指文档中每个元素:模型是指抽象化的东西. 2间隔与延迟间隔执行一段代码(函数):window.setInterv ...

  8. 由window.history.back()引发的问题

    由window.history.back()引发的问题 编写人:CC阿爸 2015-1-30 今天在这里,我想与大家一起分享由windows.history.back()引发的问题,笔者在实际开发当中 ...

  9. Knockout.Js官网学习(style绑定、attr绑定)

    Style绑定 style绑定是添加或删除一个或多个DOM元素上的style值.比如当数字变成负数时高亮显示,或者根据数字显示对应宽度的Bar.(注:如果你不是应用style值而是应用CSS clas ...

  10. (转)PHP的语言结构和函数的区别

    相信大家经常看到对比一些PHP应用中,说用isset() 替换 strlen(),isset比strlen执行速度快等. 例子: if ( isset($user) ) { //do some thi ...