Add to List 67. Add Binary
来源:互联网 发布:手机淘宝积分怎么兑换 编辑:程序博客网 时间:2024/05/22 12:00
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) {
string s="";
int la=a.length()-1,lb=b.length()-1;
int c=0;
while(la>=0||lb>=0||c==1){
//c+=(la>=0)?a[la--]:0;
//c+=(lb>=0)?b[lb--]:0;//错误,应减去‘0’,由char得到数字’
c+=(la>=0)?a[la--]-'0':0;
c+=(lb>=0)?b[lb--]-'0':0;
s=char(c%2+'0')+s;
c=c/2;
}
return s;
}
};
阅读全文
0 0
- Add to List 67. Add Binary
- Medium: Binary Tree Paths Add to List
- Add to List 563. Binary Tree Tilt
- Add to List 257. Binary Tree Paths
- Add to List 67
- leetcode104~Maximum Depth of Binary Tree Add to List
- 104.Maximum Depth of Binary Tree Add to List
- leetcode124~Binary Tree Maximum Path Sum Add to List
- LeetCode98. Validate Binary Search Tree Add to List
- 104. Maximum Depth of Binary Tree Add to List DescriptionHintsSubmissions
- LeetCode 98. Validate Binary Search Tree Add to List
- LeetCode 99. Recover Binary Search Tree Add to List
- Add to List 111. Minimum Depth of Binary Tree
- 【leedcode】 Add to List 617. Merge Two Binary Trees
- 【LeetCode】Add to List 606. Construct String from Binary Tree
- leetcode- Add to List 669. Trim a Binary Search Tree
- Add to List 617. Merge Two Binary Trees
- Add to List 617. Merge Two Binary Trees
- 手写选择题识别-生成编译opencv项目.so文件-失败
- ASP.NET网站怎么发布 Web项目程序怎么发布部署
- 《Android开发艺术探索》读书笔记-第三章 View的事件体系
- Uva
- 杭电acm 1302 蜗牛爬井
- Add to List 67. Add Binary
- python: extend (扩展) 与 append (追加) 的区别
- Loadlibrary:将Windows的动态链接库移植到Linux下
- 【JavaEE】邮箱附件下载(whitelabel Error Page)
- 0.0 reference
- 2017.8.1(1)----深搜
- 基于Java NIO的Kafka底层网络层源码和架构
- HDU 6035 Colorful Tree
- POJ1018