LeetCode(190) Reverse Bits
题目
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;
}
};
LeetCode(190) Reverse Bits的更多相关文章
- LeetCode(47)-Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- 【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 ...
- LeetCode(7)Reverse Integer
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...
- 位运算(3)——Reverse Bits
翻转32位无符号二进制整数 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (r ...
- 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 ...
- LeetCode(206) Reverse Linked List
题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...
- 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- ...
- 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. ...
- 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 ...
随机推荐
- HDU 5883 F - The Best Path 欧拉通路 & 欧拉回路
给定一个图,要求选一个点作为起点,然后经过每条边一次,然后把访问过的点异或起来(访问一次就异或一次),然后求最大值. 首先为什么会有最大值这样的分类?就是因为你开始点选择不同,欧拉回路的结果不同,因为 ...
- JS中比较的数值如何比较大小
<script type="text/javascript"> function check_num(){ var num=document.getElementByI ...
- spring事务的开启方式(编程式和声明式)
1.编程式事务:编码方式实现事务管理(代码演示为JDBC事务管理) Spring实现编程式事务,依赖于2大类,分别是上篇文章提到的PlatformTransactionManager,与模版类Tran ...
- 文件操作函数及光标,tell,truncate
一丶文件基本操作 1.打开文件 open(文件名(路径),mode = "?",encoding = "字符集") 2.文件路径: 1.绝对路径,从磁盘的根目录 ...
- C语言中的static和extern
c语言中,全局变量是一个非常重要的概念.全局变量定义在函数外,可以被所有的函数共同使用. #include <iostream> ; void display() { printf(&qu ...
- jQuery转盘插件rotate
css .rotate{ background:#aaa; padding:100px; position: relative; } .point { position: absolute; top: ...
- js读取excel数据后的时间格式转换
使用xlsx.full.min.js 获取excel的日期数据为:37858: 拿到的整数值是日期距离1900年1月1日的天数,这时需要写一个函数转换: function formatDate(num ...
- asterisk-java ami2 事件监听
asteriskServer文章1提到啦怎么获取,就不解释 asteriskServer.addChainListener(new AsteriskeventListenerInit());//整个服 ...
- 重温Javascript(三)-继承
继承 1.原型链继承 基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法.每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针.让原型 ...
- iOS 微信和支付宝关于回调处理
在支付这一块,发现讲支付集成的比较多,但是关于支付后回调处理的不多见,(当时因为这个问题懵逼了好久)就自己总结一下, 1.支付宝回调 支付宝的回调想对来说比较简单一些,因为支付宝的回调就在调起支付宝的 ...