题目:

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. Android WebView选择本地文件上传

    This sample demonstrate android webview choose file to upload. I just implement the client code ,the ...

  2. Dynamics CRM2015 非基础语言环境下产品无法新建的问题

    该现象出现在2015版本上,之前从没注意过这个问题不知道以前的版本是否存在. 我的安装包的基础语言是中文,第一张图有添加产品的按钮,切换到英文环境下后就没有了,一开始以为是系统做了隐藏处理,但用工具查 ...

  3. ROS_Kinetic_25 在ubuntu16.04使用Leap_motion并作为手势输入控制Gazebo中的机器人

    ROS_Kinetic_25 在ubuntu16.04使用Leap_motion并作为手势输入控制Gazebo中的机器人 先附上资料网址: 1.  https://developer.leapmoti ...

  4. 06 Activity 4中启动模式

    前言:改变Activity的启动模式可以清单文件AndroidManifest的Activity标签添加属性android:launchMode="standard"中修改如下图: ...

  5. Android初级教程实现电话录音

    需求:设置来电后自动录音. 首先设置一个按钮,代码很简单这里就不再给出. 建一个类,RecorderServicer extends Service package com.ydl.recorder; ...

  6. struts extjs 3.3.1 读取JSON文件

    json文件和脚本代码: jsonSrc/jsonTxt1.json, { "personInfoList": [ { "id": 0, "name& ...

  7. HTML中锚点的使用

    说到锚点,我们也许会需要稍微的思考一下,什么是锚点? 官方的答案是这样的: 那么你可能就会有下面的疑问,锚点能做什么啊? 回答就是,锚点可以理解为一个标记,一个用于而且便于寻找的标记.常用于网页制作的 ...

  8. XStream

     1.引入需要的jar包,在pom.xml中配置依赖 <dependency> <groupId>com.thoughtworks.xstream</groupId& ...

  9. RH阴性血妇女怀孕注意事项

     RH阴性血的妇女怀孕注意事项,本文主要讲解RH阴性血抗体效价检测. 第一.孕前准备:Rh阴性的妇女怀孕前,需要到血液中心或指定医院作ABO和Rh血型鉴定,并且做一次孕前血液免疫学产前检查(血型抗体检 ...

  10. HTTP请求方法

    HTTP请求方法 根据HTTP标准,HTTP请求可以使用多种请求方法. HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法. HTTP1.1新增了五种请求方法:OPTIONS, ...