lintcode ----二进制求和
2016-05-20 16:50
211 查看
想法:先转化为整数,相加后,转为二进制数
string addBinary(string& a, string& b) { // Write your code here if(a=="0"&&b=="0") return "0"; string res=""; int na=0,nb=0; int la=a.length(); int lb=b.length(); for(int i=0;i<a.length();i++) { la--; if(a[i]=='1') { na+=pow(double(2),la); } } for(int i=0;i<b.length();i++) { lb--; if(b[i]=='1') { nb+=pow(double(2),lb); } } int add=na+nb; while(add) { int tem=add%2; res+=to_string(tem); add/=2; } reverse(res.begin(),res.end()); return res; }PS:我记得之前有个类似的题目,这样写会溢出,欢迎大家亲喷指点!
相关文章推荐
- 在 LR 中如何解决Socket 接收数据的验证
- 指针实现时间复杂度为O(n*logN)的排序算法(归并排序算法)
- WPF 之 跳转
- 【Ubuntu】修改目录权限
- Android studio Gradle 进入卡住问题解决
- excel 复制粘贴时行转列
- 在嵌入式学习过程中的困惑——我一开始看什么书都看不懂,怎么才能理出一个学习顺序
- 关于Android和iOS之间个人不(chún)吐(cuì)不(tù)快(cáo)的看法
- 根文件系统移植之使用busybox
- 斐波纳契数列
- Androin学习笔记三十九:Android 中文乱码问题
- 什么才是真正程序员?
- js简单分页,可用
- iOS常用证书制作
- android 自定义View绘制计时器
- [莫比乌斯函数] BZOJ 2986 Non-Squarefree Numbers & BZOJ 2440 [中山市选2011]完全平方数
- 无线学习笔记
- 常用的adb指令(慢慢更新)
- Vue.js-----轻量高效的MVVM框架(五、计算属性)
- 数据库抽象层 pdo