write once,run anywhere
  • Home
  • Tags
  • Archives

LeetCode [190] Reverse Bits

Contents

  • Questions
  • Coding
    • python

Questions¶

Difficulty: Easy

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 is called many times, how would you optimize it?

Coding¶

python¶

# -*- coding: utf-8 -*-

class Solution(object):
    def reverseBits(self, n):
        """
        :type n: int
        :rtype: int
        """
        b = bin(n)[2:]
        s = b.zfill(32)
        r = int(s[::-1], 2)
        return r

Related Posts

  • LeetCode [1] Two Sum
  • LeetCode [2] Add Two Numbers
  • LeetCode [189] Rotate Array
  • LeetCode [58] Length of Last Word
  • LeetCode [151] Reverse Words in a String

  • « LeetCode [58] Length of Last Word
  • LeetCode [3] Longest Substring Without Repeating Characters »

Published

8月 20, 2016

Last Updated

8月 20, 2016

Category

Linux

Tags

  • LeetCode 13

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor