LeetCode题解——Add Binary
来源:互联网 发布:淘宝刷流量有用么 编辑:程序博客网 时间:2024/04/30 02:33
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
思路:最直接的想法,从最后一位开始,逐位运算。
class Solution {public://特殊输入:空字符串,10+1;11+11,1111+11,1011+11; string addBinary(string a, string b) { if(!a.size()) return b; if(!b.size()) return a; string longstr = a.size()>=b.size()?a:b; string shortstr = a.size()>=b.size()?b:a; int overtake = 0; int i=longstr.size()-1; for(int j =shortstr.size()-1; j>=0;i--,j--){ int tempvalue = int(longstr[i]-'0'+shortstr[j]-'0')+ overtake ; if(tempvalue<=1){ longstr[i] = tempvalue+'0'; overtake = 0; } else{ longstr[i] = tempvalue-2+'0'; overtake = 1; } } while(i>=0){ int tempvalue = longstr[i]-'0'+overtake; if(tempvalue<=1){ longstr[i] = tempvalue+'0'; return longstr; } else{ longstr[i] = tempvalue-2+'0'; overtake = 1; } i--; } if(overtake == 1){ longstr = '1'+longstr; } return longstr; }};
0 0
- LeetCode题解——Add Binary
- LeetCode题解:Add Binary
- LeetCode题解:Add Binary
- LeetCode题解----Add Binary
- LeetCode题解:Add Binary
- Add Binary —LeetCode
- leetcode Add Binary python 题解
- LeetCode 题解(227) : Add Binary
- Leetcode题解 67. Add Binary
- LeetCode——Add Binary
- LeetCode——Add Binary
- LeetCode题解——Add Digits
- Python学习——leetcode(Add Binary)
- Add Binary —— Leetcode(重做)
- leetcode 067 —— Add Binary
- Leetcode 67 —— Add Binary
- Leetcode——67. Add Binary
- Leetcode Problem.67—Add Binary
- iOS开发UI篇—Quartz2D简单使用(二)
- ANDROID – TOOLBAR STEP BY STEP
- poj 1276 多重背包+二进制解法
- JDBC:JDBC资源释放的细节问题
- MATLAB中feval函数的用法
- LeetCode题解——Add Binary
- 0134 Web应用中的轻量级消息队列
- Android Drawable Resource学习(十三)、其他的Drawable资源
- iOS开发UI篇—Quartz2D使用(图形上下文栈)
- 使用Visual Leak Detector帮助检测使用VS时的内存泄漏
- 使用 Nginx 的 X-Sendfile 机制控制文件访问权限(php版)
- android 联系人源码分析 新字段的添加流程
- c提高(2)
- webpack 使用插件