颠倒给定的32位无符号整数的二进制位。
例如,给定输入 43261596(二进制表示为 00000010100101000001111010011100 ),返回 964176192(二进制表示为 00111001011110000010100101000000)。
问题进阶:
如果多次调用这个函数,你将如何优化它?

详见:https://leetcode.com/problems/reverse-bits/description/

Java实现:只需要把要翻转的数从右向左一位位的取出来,如果取出来的是1,将结果res左移一位并且加上1;如果取出来的是0,将结果res左移一位,然后将n右移一位即可

public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int res=0;
for(int i=0;i<32;++i){
if((n&1)==1){
res=(res<<1)+1;
}else{
res=res<<1;
}
n=n>>1;
}
return res;
}
}

参考:https://www.cnblogs.com/grandyang/p/4321355.html

190 Reverse Bits 颠倒二进制位的更多相关文章

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

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

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

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

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

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

  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. Xpath—解决这个问题的良药

    何为良药? 因为在XML中存在一些问题和缺陷,针对这些问题就产生了响应的解决方式.如: getElementById方法在解析XML时因为一些原因适不适合的: 首先XML中每一个元素节点不一定有id属 ...

  2. B+树在NTFS文件系统中的应用

    B+树在NTFS文件系统中的应用 flyfish 2015-7-6 卷(volume) NTFS的结构首先从卷開始. 卷相应于磁盘上的一个逻辑分区,当你将一个磁盘或者磁盘的一部分格式化成NTFS,卷将 ...

  3. LeetCode 168 Excel Sheet Column Title(Excel的列向表标题)

    翻译 给定一个正整数,返回它作为出如今Excel表中的正确列向标题. 比如: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 - ...

  4. 关于disable和readonly

    我们在做网页时,难免的会因为权限或者各种原因,想让使用者看到,但是却不想让用户去对值进行更改,我们有两个选择 一.我们使用disabled将文本框禁用掉. 二.我们使用readonly使得文本框只能读 ...

  5. 爬取指定网页的源代码显示在GUI中

    建立一个GUI图形界面用来用来输入网址和代码显示的区域 #encoding=utf-8 __author__ = 'heng' #创建一个可以抓取输入网址源代码的GUI from urllib2 im ...

  6. MVC的验证(模型注解和非侵入式脚本的结合使用) .Net中初探Redis .net通过代码发送邮件 Log4net (Log for .net) 使用GDI技术创建ASP.NET验证码 Razor模板引擎 (RazorEngine) .Net程序员应该掌握的正则表达式

    MVC的验证(模型注解和非侵入式脚本的结合使用)   @HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客 ...

  7. 深度学习笔记之使用Faster-Rcnn进行目标检测 (原理篇)

    不多说,直接上干货! Object Detection发展介绍 Faster rcnn是用来解决计算机视觉(CV)领域中Object Detection的问题的.经典的解决方案是使用: SS(sele ...

  8. windows下使用mingw和msys编译GOTOBLAS和OpenBLAS

    在windows下利用msys编译openBLAS若遇到错误提示: gcc: CreateProcess : No such file or directory 问题原因参考:http://www.c ...

  9. IDEA失效的解决办法

    1.根据下图进行操作即可解决

  10. unable to instantiate activity...

    Activity跳转到Activity,后来由于项目需要将第二个Activity改成继承FragmentActivity,跳转报错...无法初始化Activity,找不到class云云.. 最后是将b ...