知之者 不如好之者, 好之者 不如樂之者

기계처럼 살지 말고, 즐기는 인간이 되자

Code/LeetCode

[LeetCode] 190. Reverse Bits (Easy/Python)

코방코 2023. 1. 15. 14:53
728x90
 

Reverse Bits - LeetCode

Reverse Bits - 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 i

leetcode.com

Above is the link to the problem.

 

Problem

The unsigned decimal integer is given.

Reverse bits of a given 32 bits unsigned integer.

 

Example

Input: n = 00000010100101000001111010011100

Output: 964176192 (00111001011110000010100101000000)

Explanation: The input binary string 00000010100101000001111010011100(2) = 43261596(10),

so return a decimal integer that is the reverse of the given binary string 964176192(10)

= 00111001011110000010100101000000(2)

 

Code

 

 

Explanation

The unsigned decimal integer is given from the problem.

We change it to a 32bit binary string and reverse it.

When it is less than 32bit long, add 0 to the front of the string until it becomes 32bit long.

Finally, reverse the string and change it to a decimal integer and return it.

 

This code gets the top 9.81% of time complexity and uses 14MB of memory.

 

 

728x90
반응형