题目

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. Net Core构建Angular4应用程序

    在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序   前言 Visual Studio 2017已经发布了很久了.做为集成了Asp.Net Core 1 ...

  2. 【OGG】OGG的单向DML复制配置(一)

    [OGG]OGG的单向DML复制配置(一) 一.1  BLOG文档结构图 一.2  前言部分 一.2.1  导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识, ...

  3. 2017 ACM Arabella Collegiate Programming Contest div2的题,部分题目写个题解

    F. Monkeying Around   维护点在多少个线段上 http://codeforces.com/gym/101350/problem/F 题意:有m个笑话,每个笑话的区间是[L, R], ...

  4. 【转载】Ubuntu16.04安装最新版nodejs

    安装最新版nodejs 更新ubuntu软件源 sudo apt-get update sudo apt-get install -y python-software-properties softw ...

  5. 完整uploadify批量上传文件插件使用

    1.首先准备uploadify的js文件,网上一搜一大堆 2.上传页面UpFilePage.aspx 关键代码: <html xmlns="http://www.w3.org/1999 ...

  6. NodeJS学习视频

    腾讯课堂初级课程 https://ke.qq.com/webcourse/index.html#course_id=196698&term_id=100233129&taid=1064 ...

  7. is 和 == 区别 编码的问题 id()函数

    一丶is 和 == 的区别 == 比较的是值 is 比较的是内存地址 #字符串 a = "abc" b = "abc" print(a == b) print( ...

  8. 织梦DeDeCMS友情链接文字显示不全

    文件:/include/taglib/flink.lib.php 把下面代码中的24改为合适的值 $attlist=”type|textall,row|24,titlelen|24,linktype| ...

  9. 超图supermap sdx数据库用sql实现空间查询

    在此介绍用sql对超图的空间数据库(sdx)进行空间查询,优点如下: 1.超图推荐的方式是用iobject,此方法要引入iobject 2.超图另一个推荐的方式是用iserver的REST接口,但we ...

  10. C++拾遗(五)——类

    类是 C++ 中最重要的特征.C++ 语言的早期版本被命名为“带类的 C(Cwith Classes)”,以强调类机制的中心作用.随着语言的演变,创建类的配套支持也在不断增加.语言设计的主要目标也变成 ...