颠倒给定的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. SqlServer 经常使用分页方法总结

    SqlServer 经常使用分页方法总结 以下演示样例总结了,SqlServer数据库 经常使用分页方法,仅供学习參考 A. 使用 RowNumber 和 Between And 组合分页: /*** ...

  2. SQL2012 尝试读取或写入受保护的内存。这通常指示其它内存已损坏

    今天打开SQL2012,突然就连接不了数据库.一開始还以为是某个server崩溃了.结果试了好几个.都还是如此,弹出提演示样例如以下: 尝试读取或写入受保护的内存.这通常仅仅是其它内存已损坏.(Sys ...

  3. 解决cell切割线不是全屏问题

    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) { [_tableView setSeparatorInset:U ...

  4. ffmpeg+libx264+facc交叉编译 实现264流录制avi文件

    默认交叉编译器已经搭建好. .. . . . . .... .... 需求: 把标准的h264流保存成avi格式的实现文件,所以须要h264的解码器,avi文件容器,传输协议类型是文件.这些会体如今f ...

  5. HashMap与HashTable的区别?

    HashMap和Hashtable的比较是Java面试中的常见问题,用来考验程序员是否能够正确使用集合类以及是否可以随机应变使用多种思路解决问题.HashMap的工作原理.ArrayList与Vect ...

  6. Class.forName() 详解

    主要功能 Class.forName(xxx.xx.xx)返回的是一个类 Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类, 也就是说JVM会执行该类的静态代码段 ...

  7. C # 踩坑记录(20190603)

    由于公司战略层需求,需要学习c#,在此仅记录相关问题,以便后期回顾. 学习路线 .NET 框架学习与C # 的关系 Visual Studio 简介及相关帮助网站(msdn) Main 方法及&quo ...

  8. ignore users and roles by filter in sql source control

    https://www.red-gate.com/hub/product-learning/sql-source-control/source-controlling-database-permiss ...

  9. unbantu16.04安装jdk

    1,解压缩jdk到指定目录 2,修改目录,方便使用 3,配置环境变量 sudo gedit /etc/environment 末尾加入以下配置(JAVA_HOME 后的路径就是jdk的文件位置) PA ...

  10. JavaScript 图片广告自动与手动的切换

    ​1.代码 <html> <head>   <script type="text/javascript" src="jquery-1.8.j ...