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. 该问题表述非常简单 ...
随机推荐
- IIS7.5修改asp的文件上传限制方法
第一.IIS7.5修改asp的文件上传限制方法 1.打开IIS 2.打开面板中的应用程序开发 asp 3.找到最后的限制属性 4.修改其中的最大请求实体主体限制的值:默认为200000字节,等于195 ...
- 使用Spark进行搜狗日志分析实例——统计每个小时的搜索量
package sogolog import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- LeetCode 56 合并区间
题目: 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [ ...
- JavaWeb项目三要素
- 2D 加速图形界面开发源代码亲写 想买来学习得加qq 313244484 20万当前代码,完整400万包写完
#include "StdAfx.h" #include "GUIFrame.h" #include <stdlib.h> #include < ...
- 最近在研究syslog日志,就说一下syslog格式吧
syslog格式:<PRI>HEADER MESSAGE syslog的消息长度:不超过1024.syslog格式举例:<15>Jul 10 12:00:00 192.168. ...
- [Java] [Singleton] [DCL][happens-before]
Singleton 只能有一个实例:必须自己创建自己的实例:必须给其他所有对象提供这一实例 实现方法 饿汉式singleton 预先加载法 class Single { private Single( ...
- python—DAY1
# user = "123"# possword = "111"# count = 0## while count < 3:# user_name = i ...
- xcfe桌面快捷键整理
转载自:https://my.oschina.net/u/565351/blog/502018 commands custom <Alt>F1:xfce4-popup-applicatio ...
- 微信小程序 获取用户信息授权
login.wxml界面 获取用户授权可以用微信提供的接口 wx.authorize, 但是获取用户信息的授权用这个接口不会弹出授权窗口 <button class="submit-b ...