1 题目

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?

Related problem: Reverse Integer

接口

public int reverseBits(int n)
uint32_t reverseBits(uint32_t n)

2 思路

简单写一下,Java的思路。Java中是没有无符号整数的,只有有符号的int(0x80000000 ~ 0x7fffffff)。

&和|操作的结合使用。

复杂度

Time: O(n)  Space: O(1)

3 代码

     public int reverseBits(int n) {
int res = 0;
for (int i = 0; i < 32; i++) {
int bit = (n >> i) & 1;
res |= bit << (31 - i);
}
return res;
}

4 总结

(n >> i) & 1是取余数的好方法,不用借助额外的空间。

5 参考

  1. leetcode
  2. stackoverflow
  3. Leetcode: Reverse Bits

lc面试准备:Reverse Bits的更多相关文章

  1. lc面试准备:Number of 1 Bits

    1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  2. 190. Reverse Bits 二进制相反数

    [抄题]: Reverse bits of a given 32 bits unsigned integer. Example: Input: 43261596 Output: 964176192 E ...

  3. [LeetCode] Reverse Bits 翻转位

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

  4. Leetcode-190 Reverse Bits

    #190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  5. 【leetcode】Reverse Bits(middle)

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

  6. LeetCode 【190. Reverse Bits】

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

  7. [leetcode] Reverse Bits

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

  8. Reverse Bits

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

  9. Java for LeetCode 190 Reverse Bits

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

随机推荐

  1. 读者汇app项目案例源码

    这款app是我花一周左右时间做出来的,一款阅读笑话,段子,糗事,脑筋急转弯,神回复,语录,谜语等的休闲娱乐app,用户除了可以浏览他人发布的内容外,自己也可以发布相关内容,和其他人一同分享有趣的内容, ...

  2. Windows2012中安装PHP-5.6.20+Apache httpd2.4.18+Composer+Laravel+MySQL5.7

    下载软件包 PHP:  http://windows.php.net/downloads/releases/php-5.6.20-Win32-VC11-x64.zip Apache httpd:  h ...

  3. HttpWebRequest结合HtmlAgilityPack实现网页form提交

    年前一个项目,需要在某个系统实现系统自动操作. 系统页面使用form提交,页面参数较多,也参数设计一系列计算逻辑,改动一个值,其他值自动改变. 传统方法使用正则表达式匹配参数,构建post参数进行请求 ...

  4. oracle 11g 安装及网络配置

    非原创,纯属笔记 安装:基本按照默认下一步安装的 1)可执行安装文件[ setup.exe ]双击安装 2):配置安全更新,取消下面的“我希望通过My Oracle Support接受安全更新(W)” ...

  5. 如何通过PS制作图片文字效果

    如图这是最终效果,下面我为大家介绍如何制作这种图片文字效果 准备一张图: 方法,步骤: 首先我们打开PHOTOSHOP,插入一张图片. 之后按键盘上面的"T"键快捷键启用文字工具, ...

  6. Java-Android 之Hello World

    1.新建一个Android Project 2.2版本的 修改values下面的内容,为: <?xml version="1.0" encoding="utf-8& ...

  7. mht文件无法打开的解决办法

    对于喜欢上网的人士来说,经常会将自己看到的好的文章保存下来,以便日后再次翻阅,保存方法有两种:一种是通过浏览器的收藏夹进行收藏,这种方式适合于能够一直上网的电脑:另一种是通过浏览器“文件->另存 ...

  8. iis5.1/6.0/7.0+ 配置url重写 无扩展名伪静态

    原文链接:http://www.cnblogs.com/diose/archive/2013/02/21/2920324.html 最近在搞url重写 遇到iis 无扩展名及html映射问题 供后人查 ...

  9. 第一章 SQL基础

    第一部分:SQL基础1. 为什么学习SQL自人类社会形成之日起,社会的运转就在不断地产生和使用各种信息(文献.档案.资料.数据等):在如今所谓的信息时代,由于计算机和互联网的作用,信息的产生和使用达到 ...

  10. Windows phone常用控件之Button

    Button类:表示一个响应 ButtonBase.Click 事件的 Windows 按钮控件. 继承层次结构: 命名空间:    System.Windows.Controls ClickMode ...