码迷,mamicode.com
首页 > 编程语言 > 详细

Leetcode 190.颠倒二进制位 By Python

时间:2018-09-24 16:39:50      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:sel   pytho   输出   reverse   符号   self   its   ase   @param   

颠倒给定的 32 位无符号整数的二进制位。

示例:

输入: 43261596
输出: 964176192
解释: 43261596 的二进制表示形式为 00000010100101000001111010011100 ,
     返回 964176192,其二进制表示形式为 00111001011110000010100101000000 。

思路

用python内置的bin()函数转为为二进制数,并用zfill()方法补足到32位,最后将字符串反转,转换为整型即可

代码

class Solution:
    # @param n, an integer
    # @return an integer
    def reverseBits(self, n):
        return int(bin(n)[2:].zfill(32)[::-1], base=2)

Leetcode 190.颠倒二进制位 By Python

标签:sel   pytho   输出   reverse   符号   self   its   ase   @param   

原文地址:https://www.cnblogs.com/MartinLwx/p/9695441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!