Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).


题目标签: Bit Manipulation

  这道题目让我们把n的bits都反转一下。我们可以通过 & 1 判断最右边一位是 0 还是 1。然后利用 >> 把n的bits向右边移动一位;利用 << 把res的位数向左边移动一位,注意res的最后一次是不需要移动的。

Java Solution:

Runtime beats 42.91%

完成日期:06/25/2017

关键词:Bit Manipulation

关键点:&, >>, << 的运用

 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) // meaning the right most bit is 1
res += 1; n = n >> 1; if(i != 31)
res = res << 1; } return res;
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 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. 190. Reverse Bits -- 按位反转

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

  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. Example 1: Input: 00000010100101000001111010011100 ...

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

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

  6. Java for LeetCode 190 Reverse Bits

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

  7. Leetcode 190 Reverse Bits 位运算

    反转二进制 class Solution { public: uint32_t reverseBits(uint32_t n) { uint32_t ans = ; ; i<; ++i,n &g ...

  8. Java [Leetcode 190]Reverse Bits

    题目描述: everse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...

  9. 【一天一道Leetcode】#190.Reverse Bits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

随机推荐

  1. Eclipse: eclipse文本文件编码格式更改(GBK——UTF-8)

    Eclipse中设置编码的方式 Eclipse工 作空间(workspace)的缺省字符编码是操作系统缺省的编码,简体中文操作系统 (Windows XP.Windows 2000简体中文)的缺省编码 ...

  2. JDBC操作数据库之修改数据

    使用JDBC修改数据库中的数据,起操作方法是和添加数据差不多的,只不过在修改数据的时候还要用到UPDATE语句来实现的,例如:把图书信息id为1的图书数量改为100,其sql语句是:update bo ...

  3. 过度拟合(overfilting)

    过拟合概念:是指分类器能够百分之百的正确分类样本数据(训练集中的样本数据),对训练集以外的数据却不能够正确分类. 原因:1:模型(算法)太过复杂,比如神经网络,算法太过精细复杂,规则太过严格,以至于任 ...

  4. BootStrap基礎知識

    BootStrap基礎知識 1. .lead //突出 .text-left //文字居左 .text-right //文字居右 .text-center //文字居中 .text-justify / ...

  5. AngularJS -- Module (模块)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 什么是AngularJS的模块 我们所说的模块,是你的AngularJS应用程序的一个组 ...

  6. 动易CMS - 添加自定义字段

    SELECT TOP 10 * FROM PE_CommonModel C INNER JOIN PE_U_xsjg U ON C.ItemID=U.ID WHERE C.Status=99 ORDE ...

  7. Hadoop安全(1)——————美团Hadoop安全实践

    http://tech.meituan.com/hadoop-security-practice.html 前言 在2014年初,我们将线上使用的 Hadoop 1.0 集群切换到 Hadoop 2. ...

  8. pycharm(windows)安装及其设置中文菜单

    pycharm(windows)安装及其设置中文菜单 1.下载 在官网(http://www.jetbrains.com/pycharm/download/#section=windows)进行下载 ...

  9. Optional乱用Empty之No value present

    前言 看到好多文章都是推荐采用Optinal的,而经常我遇到问题的时候就想:如果设计成optional的话就不会忽略这种NullPointException错误了.然而,optional并不是想用就随 ...

  10. 集群提交spark任务命令

    >>spark-submit --class WordCount  DataMining.jar /dept_ana/part-00000 /dept_ana/output/wordCou ...