leetcode reverse bits python

Reverse Bits

Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).

Follow up:
If this function is called many times, how would you optimize it?

python代码:

from string import atoi
class Solution:
# @param n, an integer
# @return an integer
     def reverseBits(self, n):
          n_str=bin(n)[2:].zfill(32)[-1::-1]    #利用bin内建函数将n转换为二进制字符串,利用切片删掉前缀0b,并将其扩充至32位,然后在反转
          n_int=string.atoi(n_str,2)       #将翻转后的二进制字符串转换为整形
          return n_int

时间: 2024-08-02 09:27:03

leetcode reverse bits python的相关文章

2016.5.16——leetcode:Reverse Bits(超详细讲解)

leetcode:Reverse Bits 本题目收获 移位(<<  >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in bin

[LeetCode]Reverse Bits

题目:Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through thi

LeetCode—Reverse Bits ,1 Bit和数字的二进制情况相关

https://leetcode.com/problems/reverse-bits/ Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as0011100101111000001

[LeetCode] Reverse Bits 翻转位

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000). Follow up:If this function i

LeetCode Reverse Bits 反置位值

题意:给定一个无符号32位整数,将其二进制形式左右反置,再以整型返回. 思路:循环32轮,将n往右挤出一位就补到ans的尾巴上. 1 class Solution { 2 public: 3 uint32_t reverseBits(uint32_t n) { 4 if( !n ) return 0; 5 uint32_t ans = 0; 6 int i; 7 for(i=0; i<32; i++ ) 8 { 9 ans <<= 1; 10 if( n & 1 ) 11 ans

[LeetCode] Reverse Bits 位操作

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000). Follow up:If this function i

190. Reverse Bits Leetcode Python

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). Follow up: If this function

190. Reverse Bits [easy] (Python)

题目链接 https://leetcode.com/problems/reverse-bits/ 题目原文 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001

[LeetCode] 190. Reverse Bits 翻转二进制位

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). Follow up:If this function