【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 ...
随机推荐
- Android开发教程AnimationDrawable逐帧播放动画
下面我们一起来看篇Android开发AnimationDrawable控制逐帧播放动画实现过程,希望文章对各位朋友带不一些帮助. 当我们点击按钮时,该图片会不停的旋转,当再次点击按钮时,会停止在当前的 ...
- Windows服务中用Timer和线程两种方式来执行定时任务
在Service服务文件夹下新建Windows服务 - TestService
- [leetcode]_Sum Root to Leaf Numbers
题目:计算一棵二叉树所有路径组成的数的总和. 思考:也是DFS的基础应用.虽然还是套着别人的DFS框架写的,但是学习通常会经历先模拟,再创新的过程. 代码: private int sum = 0; ...
- js控制div动起来
代码: <html> <head> <title>让div动的测试</title> <script language="javascri ...
- Wordpress模板标签大全
Wordpress模板基本文件 style.css 样式表文件 index.php 主页文件 single.php 日志单页文件 page.php 页面文件 archvie.php 分类和日期存档页文 ...
- How to using to code import to GL journal[AX2012]
static void THK_importLedgerJournalTrans(Args _args) { Filename fileName = "C:\\Users\\ksiu3880 ...
- [重点翻译] ASP.NET 4.6的更新 -- 本文只摘录 Web Forms的部分
原文出处:[重点翻译] ASP.NET 4.6的更新 -- 本文只摘录 Web Forms的部分 http://www.dotblogs.com.tw/mis2000lab/archive/2015/ ...
- RSA的密钥把JAVA格式转换成C#的格式
RSA算法在C#与JAVA之前的交互 在JAVA生成一对RSA私钥和公钥的时候,是以下的形式给到C#去调用: string publickey = @"MIGfMA0GCSqGSIb4DQE ...
- EMVTag系列13《脱机PIN》
DGI8010用于个人化借记贷记交易中使用的脱机PIN.数据强制要求加密.制卡数据传输过程中,此DGI采用DEK加密保护. 数据分组标识 '8010'的数据内容 要求 ...
- mysql索引合并:一条sql可以使用多个索引
前言 mysql的索引合并并不是什么新特性.早在mysql5.0版本就已经实现.之所以还写这篇博文,是因为好多人还一直保留着一条sql语句只能使用一个索引的错误观念.本文会通过一些示例来说明如何使用索 ...