leetcode:67 Add Binary-每日编程第四十三题
来源:互联网 发布:linux 网络问题排查 编辑:程序博客网 时间:2024/06/10 00:59
Add Binary
Total Accepted: 67578 Total Submissions: 261262 Difficulty: Easy
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
class Solution {public: void addBinary(string& str,string& a,string& b,int len1,int len2,bool carryBit) { if(len1==-1&&len2==-1){ if(carryBit){ str+='1'; return; }else{ return ; } } if(len1==-1){ if(!carryBit){ char ch=b[len2]; addBinary(str,a,b,len1,len2-1,false); str+=ch; }else{ if(b[len2]=='1'){ char ch='0'; addBinary(str,a,b,len1,len2-1,true); str+=ch; }else{ char ch='1'; addBinary(str,a,b,len1,len2-1,false); str+=ch; } } }else if(len2==-1){ if(!carryBit){ char ch=a[len1]; addBinary(str,a,b,len1-1,len2,false); str+=ch; }else{ if(a[len1]=='1'){ char ch='0'; addBinary(str,a,b,len1-1,len2,true); str+=ch; }else{ char ch='1'; addBinary(str,a,b,len1-1,len2,false); str+=ch; } } }else{ char ch=a[len1]+b[len2]-'0'; if(ch=='2'){ if(carryBit){ ch='1'; }else{ ch='0'; } addBinary(str,a,b,len1-1,len2-1,true); str+=ch; }else if(ch=='1'){ if(carryBit){ ch='0'; addBinary(str,a,b,len1-1,len2-1,true); str+=ch; }else{ addBinary(str,a,b,len1-1,len2-1,false); str+=ch; } }else{ if(carryBit){ ch='1'; } addBinary(str,a,b,len1-1,len2-1,false); str+=ch; } } } string addBinary(string a, string b) { int len1=a.length()-1; int len2=b.length()-1; if(a==""){ return b; }else if(b==""){ return a; } string str; addBinary(str,a,b,len1,len2,false); return str; }};
0 0
- leetcode:67 Add Binary-每日编程第四十三题
- leetcode:257 Binary Tree Paths-每日编程第四十五题
- leetcode:223 Rectangle Area-每日编程第三十三题
- leetcode:258 Add Digits-每日编程第二题
- leetcode:226 Invert Binary Tree-每日编程第七题
- leetcode:110 Balanced Binary Tree-每日编程第十九题
- [leetcode 67] Add Binary
- [LeetCode 67]Add Binary
- leetcode-67 Add Binary
- leetcode || 67、Add Binary
- LeetCode 67 Add Binary
- Add Binary - LeetCode 67
- LeetCode---(67) Add Binary
- leetcode[67]:Add Binary
- leetcode 67 Add Binary
- Leetcode# 67 Add Binary
- leetcode 67: Add Binary
- leetcode-67-Add Binary
- Jstorm与Storm中的并行度区分
- 《swift2.0 官方教程中文版》 第2章-16自动引用计数
- Hibernate学习笔记(一)
- iOS真机开机图片不显示
- App Transport Security has blocked a cleartext HTTP (http://)错误处理
- leetcode:67 Add Binary-每日编程第四十三题
- java 迭代算法使用—找节点路径
- MySQL的if,case语句使用总结
- Python 迭代器 & __iter__方法
- Android GPS定位实现,教你如何一分钟实现GPS定位
- Jvm性能调优与监控
- cocos2dx 创建lua项目 scheduler使用
- C++编程思想学习笔记---第14章 继承和组合
- ios音乐和视频合成