题目

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

Credits:

Special thanks to @ts for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

分析

题目要求将一个32位无符号整数对应的二进制串反转,求翻转后的二进制串对应的新的32位无符号整数值。

也就是说,只要我们求得该输入无符号整数的二进制表示形式,将其反转,再转换为十进制即可。

注意的问题,题目明确要求了32位,也就是说对于整数的二进制串必然长度为32,若长度不足则用0补齐。处理后再反转,求新的十进制。

另外,对于此题,我们还需要想到用位运算处理来提高运算速度。

AC代码


class Solution {
public:
//方法一
uint32_t reverseBits(uint32_t n) {
vector<int> bits;
//利用位运算求二进制序列
while (n)
{
bits.push_back(n & 1);
n = n>> 1;
} //求二进制位反转后对应的十进制数,若bits中长度不足32,以0步之
uint32_t ret = 0;
int size = bits.size();
for (int i = 0 ; i <size; ++i)
{
ret = ret + bits[i] * (1 << (32 - i - 1));
}//for
return ret; } //简洁的解法2
uint32_t reverseBits2(uint32_t n) {
uint32_t res = 0;
for (int i = 0; i < 32; ++i) {
res |= (((n >> i) & 1) << (31 - i)); }
return res;
}
};

GitHub测试程序源码

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

  1. LeetCode(47)-Reverse Bits

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

  2. 【LeetCode】190 & 191 - Reverse Bits & Number of 1 Bits

    190 - Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  3. LeetCode(7)Reverse Integer

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

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

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

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

  6. LeetCode(206) Reverse Linked List

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

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

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

  9. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

随机推荐

  1. proxy_pass http://127.0.0.1:5000; 502 bad getway

    (13: Permission denied) while connecting to upstream:[nginx] I am working with configuring django pr ...

  2. jQuery banner切换插件

    今天学写了一个基于jQuery焦点图切换插件,有不对的地方还请多多指教,不多说下面是代码: 1.引jQuery库 <script src="http://code.jquery.com ...

  3. Hart协议

    官方https://fieldcommgroup.org/technologies/hart/documents-and-downloads-hart 参考网页http://www.eeworld.c ...

  4. jeesite框架搭建中mysql数据库导入问题

    在进行mysql8.0的安装配置时,可以说是道路坎坷,之前介绍了如何安装配置mysql8.0,虽然mysql在正常情况下是很容易安装的,但是如果遇到特殊情况的时候那就需要特殊的处理. 这次我遇到的问题 ...

  5. VS2012快捷键消失

    我也是网上搜的不过我认为挺有效就自己摘录下来了,具体原作者也找不到,所以就下手了,望原谅. 开始菜单 -->所有程序-->Visual Studio 2012文件夹 --> Visu ...

  6. [转]使用 HTML5 WebSocket 构建实时 Web 应用

    HTML5 WebSocket 简介和实战演练 本文主要介绍了 HTML5 WebSocket 的原理以及它给实时 Web 开发带来的革命性的创新,并通过一个 WebSocket 服务器和客户端的案例 ...

  7. [Java][Web] Servlet中转发和重定向比较

    Servlet中页面跳转的两种方式 请求转发 使用requestDispatcher对象 request.getRequestDispatcher("path").forward( ...

  8. 一个好用的压力测试工具tsung

    一个好用的压力测试工具tsung          前段时间一直在忙各种事情,快三周没弄过引擎了,今天有点时间,正好之前写的服务器引擎也到了收尾测试的阶段,于是就研究了下怎么测试服务器压力.      ...

  9. Servlet中的初始化参数、上下文参数、以及@Resource资源注入

    配置初始化参数.上下文参数.以及使用@Resource注解进行资源注入,目的是为了降低代码的耦合度.当项目需求进行变更的时候,不需要反复更改源代码,只需更改web.xml文件即可. 一:Servlet ...

  10. ZR#330. 【18 提高 3】矿石(容斥)

    题意 题目链接 Sol 挺显然的,首先对每个矿排序 那么答案就是$2^x - 2^y$ $x$表示能覆盖到它的区间,$y$表示的是能覆盖到它且覆盖到上一个的区间 第一个可以差分维护 第二个直接vect ...