67. Add Binary【E】【44】【leetcode】
来源:互联网 发布:dota2战队数据 编辑:程序博客网 时间:2024/05/29 13:44
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
Subscribe to see which companies asked this question
按位相加,还是有些边界条件需要处理的
class Solution(object): def addBinary(self, a, b): la = len(a) lb = len(b) if la < lb: a,b = b,a la,lb = lb,la res = [''] * (max(len(a),len(b)) + 1) prp = 0 for i in xrange(1,lb+1): ta = int(a[-i]) tb = int(b[-i]) #print ta,tb,prp, resi = ta + tb + prp if resi > 1: resi -= 2 prp = 1 else: prp = 0 res[-i] = str(resi) #print res for i in xrange(lb+1,la+1): ta = int(a[-i]) resi = ta + prp if resi > 1: resi -= 2 prp = 1 else: prp = 0 res[-i] = str(resi) #print res if prp == 1: res[0] = str(prp) res = ''.join(res) return res
0 0
- 67. Add Binary【E】【44】【leetcode】
- [LeetCode]67.Add Binary
- LeetCode --- 67. Add Binary
- [Leetcode] 67. Add Binary
- [leetcode] 67.Add Binary
- 【leetcode】67. Add Binary
- [leetcode] 67.Add Binary
- [leetcode] 67. Add Binary
- 【Leetcode】67. Add Binary
- 67. Add Binary LeetCode
- 【LeetCode】67. Add Binary
- leetcode--67. Add Binary
- leetcode 67. Add Binary
- LeetCode 67. Add Binary
- LeetCode *** 67. Add Binary
- leetcode 67. Add Binary
- 【leetcode】67.Add Binary
- LeetCode 67. Add Binary
- 三级菜单渲染
- 单例模式和NSOperation的运用
- 网店系统javashop发布其开源框架,遵循GPL协议
- mapreduce的shuffle,partition,combine
- 使用MyBatis Generator自动创建代码
- 67. Add Binary【E】【44】【leetcode】
- c语言 圆周率代码
- 充电提示音
- PHP 中的命名空间
- shell中的数学计算:正式和浮点数的计算
- 使用openFileInput和openFileOutput实现Android平台的数据存储
- win10 Hyper-v 网络配置
- Android学习笔记:TabHost 和 FragmentTabHost
- awk一些简单命令