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. UI设计的重要性--避免二义性的输入提示

    昨天晚上发现了西安公路客运网上售票系统网站的密码找回系统存在安全漏洞,得出的结论是:密码找回页的漏洞:   1.用户名栏支持用户名.身份证.电话三种任意一种匹配.2.这一步是关键,密码找回问题提示栏居 ...

  2. Visual Studio Professional 2015 (x86 and x64) - DVD (Chinese-Simplified)

    文件名cn_visual_studio_professional_2015_x86_x64_dvd_6846645.isoSHA1629E7154E2695F08A3C692C0B3F6CE19DF6 ...

  3. 很励志的帖子,转来自勉,也反省一下自己写码这几年【奋斗10年,一个.NET程序员从0到拥有5系】

    http://bbs.csdn.net/topics/390833230 想想自己毕业近8年,真正写码也5年.从当初毕业时的拒绝写码,到迫不得已开始写码,是命运也好,是自己的不努力也罢.今天看来,写码 ...

  4. 黄聪:利用OpenXml生成Word2007文档(转)

    原文:http://blog.csdn.net/francislaw/article/details/7568317 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 一Op ...

  5. (C/C++) Callback Function 回调(diao)函数

    原文: http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557/Callback-Functions-Tutorial ...

  6. ARM NEON 编程系列2 - 基本指令集

    ARM NEON 编程系列2 - 基本指令集 前言 本系列博文用于介绍ARM CPU下NEON指令优化. 博文github地址:github 相关代码github地址:github NEON指令集 主 ...

  7. php 信号量

    一些理论基础: 信号量:又称为信号灯.旗语 用来解决进程(线程同步的问题),类似于一把锁,访问前获取锁(获取不到则等待),访问后释放锁. 临界资源:每次仅允许一个进程访问的资源. 临界区:每个进程中访 ...

  8. SaveFileDialog的用法

    #region 保存对话框 private void ShowSaveFileDialog() { //string localFilePath, fileNameExt, newFileName, ...

  9. 查询oracle中所有用户信息

    1.查看所有用户:select * from dba_users;   select * from all_users;   select * from user_users; 2.查看用户或角色系统 ...

  10. js 事件函数中的参数带换行符或换行标签都不能起作用的解决方法

    把问题参数值赋给标签的属性data-value,通过属性值获取参数值.