[LeetCode] 190. 颠倒二进制位

题目链接:https://leetcode-cn.com/problems/reverse-bits/

题目描述:

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

示例:

示例 1:

输入: 00000010100101000001111010011100
输出: 00111001011110000010100101000000
解释: 输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596,
      因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000。

示例 2:

输入:11111111111111111111111111111101
输出:10111111111111111111111111111111
解释:输入的二进制串 11111111111111111111111111111101 表示无符号整数 4294967293,
      因此返回 3221225471 其二进制表示形式为 10101111110010110010011101101001。

思路:

思路一:库函数

class Solution:
    # @param n, an integer
    # @return an integer
    def reverseBits(self, n):
        return int("{:032b}".format(n)[::-1], 2)

思路二:位运算

猜你喜欢

转载自www.cnblogs.com/powercai/p/11370081.html