【Add binary】cpp
题目:
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的更多相关文章
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
- 【Same Tree】cpp
题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
- 【Text Justification】cpp
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- 【Divided Two】cpp
题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...
- 【Multiply Strings】cpp
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- 【Minimum Window】cpp
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
随机推荐
- 《cocos2d-x游戏开发》—— lua学习总结(一)数组的使用
在lua中,数组是用table来实现的. 1.数组的定义: self.itemArrays = {}; --作为数组来使用的表itemArrays 2. 数组插入一条数据: local showIte ...
- [原]sdut2624 Contest Print Server (大水+大坑)山东省第四届ACM省赛
本文出自:http://blog.csdn.net/svitter 原题:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&am ...
- Linux多线程编程阅读链接
1. 进程与线程的一个简单解释(阮一峰) 2. linux 多线程编程 3. Linux 的多线程编程的高效开发经验 (IBM)
- 控制不能离开Finally子句主体
1.在try{}catch{}finally{}的结构中不可以将返回语句放置在finally的主体当中2.如果在catch{}中有向上一级从新抛出异常操作,在finally{}之后的语句将不会执行 3 ...
- Cannot change version of project facet Dynamic web的解决方法
用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servlet还是2.3的,而一般现 ...
- [leetcode]_String to Integer (atoi)
非常考虑思维全面性的一道题,考验是否能够考虑本问题的方方面面. 题目:将一个string转换为int.实现函数atoi()的功能. 先应该明确atoi()有哪些特殊功能:(正常的正负数情况我就不列了) ...
- sail.js学习 - 安装篇
导言: 最近在学习sails.js(http://sailsjs.org/),因为用的人不多,资料较少,故写些自己的学习过程.因自己也是初学node.js,有问题大家指出. 介绍: sails.js的 ...
- php变量那些事:学习过程中遇到的关于php变量的有趣的问题(不断发现不断更新)
不断发现……,不断更新……,不断寻找答案……例子的测试环境:php5.3,win7,64位Num1:<?$a=array(1,2,3,4,5,6); $b=$a;$m=memory_get_us ...
- appcan weixin 开发
登录微信开放平台:https://open.weixin.qq.com/ 管理中心,创建移动应用,ps:创建应用需要审核,其中 应用包名 需与在线打包安卓时候的 自定义包名一致. 开放平台 应用申请 ...
- Android图表
最近需要用到Android里面的折现图,因此在这方面也去做了一些调研.总体发现Android对报表的支持还是非常好的.总体上去研究了两个实现方案,一个是利用Android提供的的AChartEngin ...