Leetcode Reverse Bits Python

Source: Internet
Author: User

Reverse Bits

Reverse bits of a given the unsigned integer.

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

Follow up:
If This function is called many times, what would you optimize it?

Python code:

From string import atoi
Class Solution:
# @param n, an integer
# @return An integer
def reversebits (self, N):
N_str=bin (n) [2:].zfill (+) [ -1::-1] #利用bin内建函数将n转换为二进制字符串, delete the prefix 0b using a slice, expand it to 32 bits, and then reverse
N_int=string.atoi (n_str,2) #将翻转后的二进制字符串转换为整形
Return N_int

Leetcode Reverse Bits Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.