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

leetcode reverse bits python的更多相关文章

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

    leetcode:Reverse Bits 本题目收获 移位(<<  >>), 或(|),与(&)计算的妙用 题目: Reverse bits of a given 3 ...

  2. [LeetCode] Reverse Bits 翻转位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  3. [leetcode] Reverse Bits

    Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (re ...

  4. LeetCode Reverse Bits 反置位值

    题意:给定一个无符号32位整数,将其二进制形式左右反置,再以整型返回. 思路:循环32轮,将n往右挤出一位就补到ans的尾巴上. class Solution { public: uint32_t r ...

  5. [LeetCode] Reverse Bits 位操作

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  6. leetcode Reverse Integer python

    class Solution(object): def reverse(self, x): """ :type x: int :rtype: int "&quo ...

  7. lc面试准备:Reverse Bits

    1 题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...

  8. 【LeetCode】190. Reverse Bits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...

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

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

随机推荐

  1. jquery radio的取值 radio的选中 radio的重置

    radio 按钮组, name=”sex”. <input type="radio" name="sex" value="Male"& ...

  2. LintCode "Binary Representation"

    Not hard to think of a solution. But the key is all details. class Solution { public: /** *@param n: ...

  3. 剑指offer系列38----滑动窗口的最大值(不懂????????????????????????????????????????????????)

    [题目] 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6 ...

  4. bzoj2152 聪聪可可

    Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...

  5. 黄聪:Mysql数据库还原备份提示MySQL server has gone away 的解决方法(备份文件数据过大)

    使用mysql做数据库还原的时候,由于有些数据很大,会出现这样的错误:The MySQL Server returned this Error:MySQL Error Nr. MySQL server ...

  6. 2. hdfs

    一.Hdfs的shell 所有hadoop的fs的shell均用uri路径作为参数 uri格式:schema://authority/path.hdfs的schema是hdfs.其中,schema和a ...

  7. 发现木马C:\windows\system32\FastUserSwitchingCompatibilityex.dll

    而且用安全狗还隔离不了

  8. C++ 局部变量的析构

    http://blog.chinaunix.net/uid-52437-id-2108747.html 在一个函数内,申明一个局部类变量.则这个变量什么时候析构呢? 并不是在函数退出,释放栈空间时候析 ...

  9. 4 c#

    /直接小写transform表示获取当前脚本所挂载的游戏对象身上的Transform组件 Vector3 p= transform.position;//     transform组件上的位置属性 ...

  10. keil逻辑分析仪的使用:

    http://blog.sina.com.cn/s/blog_4e0175750101kt13.html