“This is the third day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”

describe

Reverse bits of a given 32 bits unsigned integer.

Note:

  • Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer’s internal binary representation is the same, whether it is signed or unsigned.
  • In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer -1073741825.

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

Example 1:

Input: n = 00000010100101000001111010011100
Output:    964176192 (00111001011110000010100101000000)
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.
Copy the code

Example 2:

Input: n = 11111111111111111111111111111101
Output:   3221225471 (10111111111111111111111111111111)
Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111.
Copy the code

Note:

The input must be a binary string of length 32
Copy the code

parsing

Is the bits of a given 32 – bit unsigned number. For example, 43261596 (00000010100101000001111010011100) binary representation for a given input integer, return 964176192 (binary representation of 00111001011110000010100101000000).

The title also asks me more: how can I optimize this function if it is called multiple times?

The simplest is to use the built-in function, the bin function to reverse the binary of n, and then

answer

class Solution:
    def reverseBits(self, n):
        b = bin(n)[:1:-1]
        return int(b + '0'*(32-len(b)), 2)

        	      
		
Copy the code

The results

Given in the Python online submission for Reverse Bits. Memory Usage: Given in the Python online submissions for Reverse Bits.Copy the code

parsing

Find each digit of the binary of n from back to front, according to the question, and then use the bit operation to calculate the result:

  • Cycle of 32
  • Each time the loop moves the result variable to the left, multiplying by 2. If the current binary number is 1, you can add result by 1, and then move n to the right by 1, dividing by 2
  • Result is obtained at the end of the traversal

answer

class Solution:
    def reverseBits(self, n):
        result = 0
        for _ in range(32):
            result <<= 1
            if n&1 :
                result += 1
            n >>= 1
        return result
Copy the code

The results

Given in the Python online submission for Reverse Bits. Memory Usage: Given in the Python online submissions for Reverse Bits.Copy the code

Original link: leetcode.com/problems/re…

Your support is my biggest motivation