leetcode -- Add Binary -- 简单要了解
来源:互联网 发布:mac怎么传照片给iphone 编辑:程序博客网 时间:2024/06/06 11:36
https://leetcode.com/problems/add-binary/
知道二进制加法原理即可,这里只需要知道进位是除数,余数是结果就行。最后不要忽略reg里面的值
class Solution(object): def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ if not a: return b elif not b: return a tmp_str = ''.join(['0'] * abs(len(a) - len(b))) if tmp_str: if len(a) > len(b): b = tmp_str + b else: a = tmp_str + a reg = 0 res = '' for i in xrange(len(a) - 1, -1, -1): tmp = int(a[i]) + int(b[i]) + reg mod, inc = tmp % 2, tmp / 2 res = str(mod) + res #print (reg,tmp, mod, inc, res) reg = inc if reg != 0: res = '1' + res return res
看这个博客。整理的解题思路
http://c4fun.cn/blog/2014/03/20/leetcode-solution-02/
0 0
- leetcode -- Add Binary -- 简单要了解
- leetcode -- Plus One--与add binary类似--要了解
- LeetCode: Add Binary
- [Leetcode] Add Binary
- LeetCode Add Binary
- Leetcode: Add Binary
- [Leetcode] Add Binary
- LeetCode : Add Binary
- LeetCode Add Binary
- [LeetCode] Add Binary
- leetcode 58: Add Binary
- leetcode Add Binary
- [LeetCode] Add Binary
- 【leetcode】Add Binary
- [LeetCode]Add Binary
- [leetcode]Add Binary
- LeetCode-Add Binary
- [leetcode] Add Binary
- MyEclipse2014安装插件的几种方式(适用于Eclipse或MyEclipse其他版本)
- dataTables-使用详细说明整理,还有各种参数、回调方法
- hdu 5567 sequence1(水)
- Scala之for循环和函数进阶
- glibc 中的 __attribute__ 关键字
- leetcode -- Add Binary -- 简单要了解
- 从头认识java-12.1 为什么需要RTTI(Run-Time Type Identification)?
- 使用Fragment实现底部菜单栏
- hdu 5568 sequence2(dp + 大数)
- jqueryHTML相关方法
- Fragment销毁时replace和add两个方法的区别
- android有关图片的操作(一)
- 指针函数和函数指针
- Arcgis Server服务中rest服务和wms服务的对应关系