python leetcode 颠倒二进制数
我的做法,,这个题在于必须补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 颠倒二进制数的更多相关文章
- python leetcode 1
开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...
- 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 ...
- python LeetCode 两数相除
近一个月一直在写业务,空闲时间刷刷leetcode,刷题过程中遇到了一道比较有意思的题目,和大家分享. 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使 ...
- LeetCode. 颠倒二进制位
题目要求: 颠倒给定的 32 位无符号整数的二进制位. 示例: 输入: 00000010100101000001111010011100 输出: 001110010111100000101001010 ...
- python leetcode 日记--231. Power of Two
题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...
- python leetcode 日记 --Contains Duplicate --217
题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...
- 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 ...
- Python LeetCode
Python不熟悉 不同的做法 404. Sum of Left Leaves 这是我的做法,AC. class Solution(object): res = 0 def recursive(sel ...
- python(leetcode)-重复元素算法题
leetcode初级算法 问题描述 给定一个整数数组,判断是否存在重复元素. 如果任何值在数组中出现至少两次,函数返回 true.如果数组中每个元素都不相同,则返回 false. 该问题表述非常简单 ...
随机推荐
- mybatis Interceptor拦截器代码详解
mybatis官方定义:MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...
- H5样式(个人使用)
@*定义全局样式*@ <style> body, ul, li, p, h1, h2, h3, h4, h5, h6, hr, span, form, fieldset, table, t ...
- idea开发maven项目热加载
JavaWeb项目,尤其是一些大型项目,在开发过程中,启动项目耗费的时间就不短.大大的拖慢了开发速度!在这分享一种不需要插件就能实现热加载的方法! 默认已经创建好一个Maven项目 点击此按钮 点击 ...
- Problem 5: Smallest multiple
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any rema ...
- mongo中的游标与数据一致性的取舍
除了特殊注释外,本文的测试结果均基于 spring-data-mongodb:1.10.6.RELEASE(spring-boot-starter:1.5.6.RELEASE),MongoDB 3.0 ...
- php websocket
php websocket项目开发,推荐使用:Workerman 本片内容使用Workerman实现了简单的及时聊天功能,具体代码如下: <?php // phpinfo(); header(' ...
- Linux基础命令--date
date命令格式用法 yao@yao:~/shells/tmp$ date +%Y%m%d20190405 yao@yao:~/shells/tmp$ date +%Y%m%d%H%M%S201904 ...
- Linux 环境下umount, 报 device is busy 的问题分析与解决方法
在Linux环境中,有时候需要挂载外部目录或硬盘等,但当想umount时,却提示类似“umount:/home/oracle-server/backup:device is busy”这种提示. 出现 ...
- JavaScript中的各种宽高总结
window和document首先我们来高清两个概念: window和document的区别是什么? window.location和document.location是一样吗?第一个问题 ...
- my first homepage
<!DOCTYPE html><html><head><style type="text/css">p{ text-indent:2 ...