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

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Hide Tags

Bit Manipulation

 
2 分析
通过题191的思路,我们可以获得每位的数据,这样反转也就很方便了,刚开始以为要存起来,反过来赋值一遍,结果不用,两个移位的方向不一样就行了。
ps java里面向高位移位时,直接丢弃,不保存。
 
3 代码
    public int reverseBits(int n) {
int reverseN = 0;
int b;
for (int i = 0; i < 32; i++) {
b = n&1;
reverseN = (reverseN << 1) + b;
n = n >> 1;
}

另外,有个问题,不定义b的话,直接用

 reverseN = (reverseN << 1) + n&1;得不到结果,结果全部是0,原因未知。。

[leet code 190]reverse bits的更多相关文章

  1. LeetCode 190. Reverse Bits (算32次即可)

    题目: 190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432 ...

  2. [LeetCode] 190. Reverse Bits 颠倒二进制位

    Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 ...

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

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

  4. LeetCode 【190. Reverse Bits】

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

  5. Java for LeetCode 190 Reverse Bits

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

  6. 190. Reverse Bits -- 按位反转

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

  7. Leetcode 190. Reverse Bits(反转比特数)

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

  8. 190. Reverse Bits

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

  9. 【LeetCode】190. Reverse Bits

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

随机推荐

  1. [ES]elasticsearch章3 ES写入过程解析

    Elasticsearch的写 Elasticsearch采用多Shard方式,通过配置routing规则将数据分成多个数据子集,每个数据子集提供独立的索引和搜索功能.当写入文档的时候,根据routi ...

  2. 2018年设计师都在用的PS切图插件--摹客iDoc

    终于找到你,我梦寐以求的PS切图插件.曾几何时,设计师在完成设计稿之后高效的输出标注切图一直是设计师的噩梦.为什么这么说呢?开发要的那么多尺寸,我到底该怎么切图?iPhone的版本已经不少了,更别提安 ...

  3. HttpURLConnection 返回汉字乱码(全是问号)

    public static String doPost(String urlStr, Map<String, Object> paramMap) throws Exception { UR ...

  4. javase高级技术 - 反射

    在说反射之前,必须得先说说java的类加载器,类加载器的定义:将.class文件加载到内在中,并为之生成对应的Class对象. 一般有三种 1 Bootstrap ClassLoader 根类加载器也 ...

  5. SpringMVC学习八 @ResponseBody注解

    (一)在方法上只有@RequestMapping 时,无论方法返回值是什么认为需要跳转,代码实例如下 @RequestMapping("demo10") public People ...

  6. springMVC学习 七 视图解析器

    在springMVC中,如果不配置视图解析器,会走默认的视图解析器,但是有时候配置视图解析器,还有一定的作用 <bean id="viewResolver" class=&q ...

  7. Win8.1无法安装更新,提示0x800*****错误的解决方法

    Win8.1无法安装更新,提示0x800*****错误的解决方法   注:本教程同样适用于Win10系统 有时候Win8.1某个系统文件的损坏会导致无法安装Windows更新,表现为Windows更新 ...

  8. shell入门练习

    **定义局部变量, 局部变量在退出Shell客户端时会失效** **单引号:原样输出** **双引号:如果里面有变量,会输出变量** **没有引号:输出变量** 可以在调用脚本的时候给脚本传递参数,脚 ...

  9. 52.tableViewCell重用机制避免重复显示问题

    表刷新超出页面显示的内容会重复出现 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSInd ...

  10. s5-13 RIP 为什么会 衰败

    DV路由可能遇到的问题 路由环路( routing loop) 计数到无穷问题( Count to infinite) 收敛慢的问题( slow Convergence ) 相信错误的路由信息导致 好 ...