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. 该问题表述非常简单 ...
随机推荐
- 未知高度的div自适应图片高度
<div style="background-image: url(http://your-image.jpg);"> <img src="http:/ ...
- 转 Redis 总结精讲 看一篇成高手系统-4
转 Redis 总结精讲 看一篇成高手系统-4 2018年05月31日 09:00:05 hjm4702192 阅读数:125633 本文围绕以下几点进行阐述 1.为什么使用redis 2.使用r ...
- s5p6818开发板uboot网络开通
手上的开发板网络默认是不通的,但是通过阅读uboot源码,发现uboot源码中,是有对这个网络的初始化的实现的函数的,只不过是没有调用而已,所以,要手动调用这个函数,把板子的网络调通: 首先是遇到了这 ...
- IT技术
一.通信网络 TCP/IP协议 路由交换技术 二.编程语言 C/C++ python JAVA 三.数据库 关系型数据库 (1)MySQL MySQL学习笔记一 MySQL学习笔记二 2. 非关系 ...
- maven profile实现多环境配置
每次项目部署上线都需要手动去修改配置文件(比如数据库配置,或者一个自定义的配置)然后才能打包,很麻烦,网上找到 maven profile可以完成这个工作,记录如下: 环境:eclipse + spr ...
- Linux安装软件、python开发环境
软件安装与卸载 更新Ubuntu软件下载地址 1. 寻找国内镜像源 所谓的镜像源:可以理解为提供下载软件的地方,比如Android手机上可以下载软件的91手机助手:iOS手机上可以下载软件的AppSt ...
- CSS中的颜色问题
css颜色: CSS 颜色 颜色是通过对红.绿和蓝光的组合来显示的 颜色值 CSS 颜色使用组合了红绿蓝颜色值 (RGB) 的十六进制 (hex) 表示法进行定义.对光源进行设置的最低值可以是 0(十 ...
- 函数QFileSystemModelPrivate::_q_fileSystemChanged
由于Qt的体系过于庞大,即使是某个模块,分析起来也十分困难.对于QFileSystemModel,我们在这里对单个重要函数慢慢分析 1 /*! \internal The thread has rec ...
- KMP算法详细分解
1. 引言 给定一个主串(以 S 代替)和模式串(以 P 代替),要求找出 P 在 S 中出现的位置,此即串的模式匹配问题. Knuth-Morris-Pratt 算法(简称 KMP)是解决这一问题的 ...
- 周强 201771010141 《面向对象程序设计(java)》第九周实验总结
实验部分 1.实验目的与要求 (1) 掌握java异常处理技术: (2) 了解断言的用法: (3) 了解日志的用途: (4) 掌握程序基础调试技巧: 2.实验内容和步骤 实验1:用命令行与IDE两种环 ...