题目:

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).

Follow up:
If this function is called many times, how would you optimize it?

Related problem: Reverse Integer

思路:

  • 题意:给定一个无符号数,把他的二进制位转换过来
  • 考虑首先把原数字右移,然后得到右移的数字,赋值给新数字,然后左移动
  • -

代码:

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++, n >>= 1){
            res = res << 1 | (n & 1);
        }
        return res;
    }
}

LeetCode(47)-Reverse Bits的更多相关文章

  1. LeetCode(190) Reverse Bits

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

  2. LeetCode(7)Reverse Integer

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...

  3. 位运算(3)——Reverse Bits

    翻转32位无符号二进制整数 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (r ...

  4. LeetCode(151) Reverse Words in a String

    题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...

  5. LeetCode(206) Reverse Linked List

    题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...

  6. LeetCode(92) Reverse Linked List II

    题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1- ...

  7. LeetCode(25)Reverse Nodes in k-Group

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  8. LeetCode(47):全排列 II

    Medium! 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 这道 ...

  9. LeetCode(47)Permutations II

    题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...

随机推荐

  1. [ExtJS5学习笔记]第二十四节 Extjs5中表格gridpanel或者表单数据后台传输remoteFilter设置

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/39667533 官方文档:http://docs.sencha.com/extjs/5. ...

  2. Android广播接收器Broadcast Receiver-android学习之旅(十二)

    首先继承BroadcastReceiver类,并在manifest中注册 public class MyReceiver extends BroadcastReceiver { public MyRe ...

  3. 【一天一道LeetCode】#299. Bulls and Cows

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...

  4. Android的ViewFlipper-android学习之旅(三十五)

    ViewFlipper的简介 ViewFlipper继承于ViewAnimator,它和AdapterViewFlipper有着许多的相似的地方. 代码示例 package peng.liu.test ...

  5. iOS中 动态启动图GIF的简单设置 韩俊强的博客

    // 设定位置和大小 CGRect frame = CGRectMake(50,340,[UIScreen mainScreen].bounds.size.width / 2,[UIScreen ma ...

  6. Java-IO之总框架

    在Java IO中我们会经常提到输入流和输出流,流是一种抽象的数据总称,本质是能够进行数据的传输.按照流的方向分为:输入流和输出流.按照流中处理数据的单位,可以将其区分为:字节流和字符流.在Java中 ...

  7. catalina.sh设置JAVA_HOME后还无法解决更换JDK有关问题

    catalina.sh设置JAVA_HOME后还无法解决更换JDK问题 表示linux已经安装默认的JDK,需要查找配置文件,更换JDK路径为指定的路径 在root用户下 使用echo $PATH 查 ...

  8. MO_GLOBAL包中一些过程和函数的使用

    DECLARE V_CURRENT_ORG_ID NUMBER; V_ACCESS_MODE VARCHAR2(2); V_OU_COUNT NUMBER; V_ORG_ID NUMBER; V_MO ...

  9. spring与mybatis(oracle)整合

    今天闲着无聊把项目拆解开,抽出了spring与mybatis部分.做了个demo,希望对初学者有些帮助,另外整个demo是从项目中完整剥离下来的,里面的架构大家也可以参考一下. 先是完整的项目图 首先 ...

  10. Java Swing 之JTable及其简单的用法

    我们都知道JTable需要使用一个Model配合才能更好地发挥其作用.而使用Model有好多种方法,但是难易程度却大大不同,比如说我们使用AbstractTableModel接口要实现里面的好多方法, ...