我的做法,,这个题在于必须补0
def reverseBits(n):

    num=32-len(bin(n)[2:])
m = bin(n)[2:][::-1]
if num > 0:
for i in range(num):
m=m+'0'
print(m,int(m,2)) return m

 看到前几做法

        nums=bin(n)
nums=nums.lstrip('0b')
nums=nums.zfill(32)
#zfill 一直没找到。。。。原来是这个
nums=nums[::-1]
return int(nums,2)

zfill()用法:

描述

Python zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0。

语法

zfill()方法语法:

str.zfill(width)

摘自:http://www.runoob.com/python/att-string-zfill.html

另一种,目前第一的做法
 
         binNum = bin(n)
binStr = str(binNum)
binStr = binStr[2:]
#rjust..对应应该有ljust#
reverseStr = binStr.rjust(32, '')
reverseStr = reverseStr[::-1] reverseNum = int(reverseStr, 2)
return reverseNum

描述

rjust() 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串。

语法

rjust()方法语法:

str.rjust(width[, fillchar])

参数

  • width -- 指定填充指定字符后中字符串的总长度.
  • fillchar -- 填充的字符,默认为空格。

摘自:http://www.runoob.com/python3/python3-string-rjust.html

python leetcode 颠倒二进制数的更多相关文章

  1. python leetcode 1

    开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...

  2. python leetcode 日记 --Contains Duplicate II --219

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  3. python LeetCode 两数相除

    近一个月一直在写业务,空闲时间刷刷leetcode,刷题过程中遇到了一道比较有意思的题目,和大家分享. 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使 ...

  4. LeetCode. 颠倒二进制位

    题目要求: 颠倒给定的 32 位无符号整数的二进制位. 示例: 输入: 00000010100101000001111010011100 输出: 001110010111100000101001010 ...

  5. python leetcode 日记--231. Power of Two

    题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...

  6. python leetcode 日记 --Contains Duplicate --217

    题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  7. Python [Leetcode 345]Reverse Vowels of a String

    题目描述: Write a function that takes a string as input and reverse only the vowels of a string. Example ...

  8. Python LeetCode

    Python不熟悉 不同的做法 404. Sum of Left Leaves 这是我的做法,AC. class Solution(object): res = 0 def recursive(sel ...

  9. python(leetcode)-重复元素算法题

    leetcode初级算法 问题描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 该问题表述非常简单 ...

随机推荐

  1. python之路-变量

    变量定义的规则: - 变量名只能是字母.数字.或下划线的任意组合 - 变量名的第一个字符不能是数字 - 以下关键字不能声明为变量名  ['and','as','assert','break','cla ...

  2. virtualenv与virtualenvwrapper的配置

    virtualenv是用来创建python虚拟环境的一个工具(python的Scripts目录下virtualev.exe文件),virtualenvwrapper是用来 便于管理virtualenv ...

  3. Visual Assist X 中使用doxygen的模板注释

    http://blog.csdn.net/dhifnoju/article/details/44947213 Doxygen是一种开源跨平台的,以类似JavaDoc风格描述的文档系统,完全支持C.C+ ...

  4. 第一个HTML文档

    属性 和 值 1.作用 用来修饰元素 ex:让 p 标记的文本水平居中对齐 <p>Hello World</p> 2.语法      1.属性的声明必须位于开始标记里      ...

  5. 更改TestStep的request header和获取TestStep的response header

    更改TestStep的request header for example def userId = "xxxxxxxxxxxxx" def request = context.t ...

  6. 信息技术手册可视化进度报告 基于BeautifulSoup框架的python3爬取数据并连接保存到MySQL数据库

    老师给我们提供了一个word文档,里面是一份信息行业热词解释手册,要求我们把里面的文字存进数据库里面,然后在前台展示出来. 首先面临的问题是怎么把数据导进MySQL数据库,大家都有自己的方法,我采用了 ...

  7. 最近学习了Sqlite3数据库,写一下操作应用以及命令

    首先使用Flask-SQLAlchemy管理数据库 使用pip安装:pip install flask-sqlalchemy 接着要配置数据库,定义模型 关于数据库的操作就不再写了.... 使用Fla ...

  8. visual c++如何显示行号

    工具 -> 选项 -> 文本编辑器

  9. mysql导出导入数据无权限

    问题:The MySQL server is running with the --secure-file-priv option so it cannot execute this statemen ...

  10. full visualization vs part virtualization

    https://stackoverflow.com/questions/21462581/what-is-the-difference-between-full-para-and-hardware-a ...