题目

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准备:使用Hyper-V安装Ubuntu Server 16.10

    NET Core准备:使用Hyper-V安装Ubuntu Server 16.10 概述 Hyper-V是微软的一款虚拟化产品,和VMWare一样采用的hypervisor技术.它已经被内嵌到Win1 ...

  2. Batch梯度下降

    1.之前讲到随机梯度下降法(SGD),如果每次将batch个样本输入给模型,并更新一次,那么就成了batch梯度下降了. 2.batch梯度下降显然能够提高算法效率,同时相对于一个样本,batch个样 ...

  3. Vsftp设置为PASV mode(被动模式传送)

    首先配置vsftpd.conf文件: #vi /etc/vsftpd/vsftpd.conf 在文件的末尾加上: pasv_enable=YES pasv_max_port=30010 pasv_mi ...

  4. PHP正则表达式 - 附录(常用正则表达式)

    常用正则表达式附录I 平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: "^\d+$" //非负整数(正整数 + 0) "^[0-9]*[1- ...

  5. gulp管理angular2项目 配置文件

    目录结构: projectName |_ src |_ app |_ index.html |_ main.ts |_ systemjs.config.js |_ gulpfile.js |_ pac ...

  6. 简述null undefined NaN的异同

    1. 类型类型分析: JS中数据类型有5种:string,number,boolean,undefined,object,前四种值类型(基础数据类型),object是引用类型 var a1; //un ...

  7. 问题:java.sql.SQLException: No value specified for parameter 1

    解决方案:没有指定参数 String user = req.getParameter("user"); String pwd = req.getParameter("pw ...

  8. 关于vue-resource 转变成axios的过程

    在做东钿贷后系统的时候,我选择了vue-resource这个插件作为与服务器沟通工具,但是听说前端同行说vuejs2.0已经不在维护vue-resource了,vuejs2.0 已经使用了axios了 ...

  9. 两个页面实现mui轮播图与选项卡结合

    index.html页面 <!DOCTYPE html><html><head> <meta charset="utf-8"> &l ...

  10. Windows Azure 配置Active Directory 主机(2)

    前一篇概况给大家介绍了,在云端部署一台DC 需要满足一些条件,接下来进入正题,云端VM安装域控制器具体步骤. 步骤1 :验证 主DC 的静态 IP 地址 1.登录到 Corp 网络上的 主DC. 2. ...