我的做法,,这个题在于必须补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. Dubbo框架设计

    各层说明 config配置层:对外配置接口,以 ServiceConfig, ReferenceConfig 为中心,可以直接初始化配置类,也可以通过 spring 解析配置生成配置类 proxy服务 ...

  2. 活代码LINQ——09

    一.代码 ' Fig. 9.7: LINQWithListCollection.vb ' LINQ to Objects using a List(Of String). Module LINQWit ...

  3. php不用正则表达式实现身份证号验证详解

    写了一个身份证号验证类,php小白,写的不好,欢迎各位大神给我多提意见和建议 身份证号分为四部分,第一部分是前6位为地址码,7-14位是出生日期,15-17位是同一地方同一天出生的男孩为奇数,女孩是偶 ...

  4. SpringBoot 2.0 pom.xml 配置(热启动)

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...

  5. io复用select方法编写的服务器

    摘要:io多路复用是通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般都是读就绪或者写就绪),就能通知应用程序进行相应的读写操作.select函数作为io多路复用的机制,第一个参数nfds是f ...

  6. admin 显示多对多字段

    class BookAdmin(admin.ModelAdmin): def 作者(self, object): return [a.name for a in object.author.all() ...

  7. in条件后面有多个字段,in后面只能有一个字段 Operand should contain 1 column(s)

    今天在sql测试的时候发现了这个错误:Operand should contain 1 column(s). 原因是in条件后面有多个字段,in后面只能有一个字段.

  8. LimeSDR 上手指南

    原文链接:https://mp.weixin.qq.com/s/so4XzPaYtzAvgbjarm_9fg 有问题可以在公众号或者这里留言

  9. 为什么想起开通blog?

    为什么想起开通博客 2016年跨年夜写年终总结时,曾对自己许下愿,要成为一个会讲“故事”的人,无奈口才不行,写字也不好看,所以只能在电脑上码码字代替了. 在我看来,这“故事”该有许多种含义:首先,电子 ...

  10. Java判断字符串是否有重复

      检测是否重复: public static boolean checkDifferent(String iniString) { boolean isbool = false; char[] ch ...