leetcode: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 as00111001011110000010100101000000).
Follow up:
If this function is called many times, how would you optimize it?
Related problem: Reverse Integer
分析:题意为反转给定32位无符号整型数的位
思路:我们只需将原整型数从右到左一个一个取出来,然后一个个加到新数的最低位中即可
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t s=0;
for(int i=0;i<32;i++){
if(n&1==1){
n>>=1;
s=(s<<=1)+1;
}
else{
n>>=1;
s=(s<<=1);
}
}
return s;
}
};
或可参考更简洁做法:
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t res = 0;
for (int i = 0; i < 32; ++i) {
res |= (((n >> i) & 1) << (31 - i));
}
return res;
}
};
其他可参考方法:
#include<iostream>
using namespace std; class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t m=0;
for(int i=0;i<32;i++){
m<<=1;
m = m|(n & 1);
n>>=1;
}
return m;
}
}; int main()
{
uint32_t n = 1;
Solution sol;
cout<<sol.reverseBits(n)<<endl;
return 0;
}
或:
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t m = 0;
for (int i = 0; i< 32 ; i++,n/=2)
m = (m<<1) + (n%2);
return m;
}
};
leetcode:Reverse Bits的更多相关文章
- LeetCode OJ:Reverse Bits(旋转bit位)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- LeetCode 192:Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- LeetCode 190. Reverse Bits (反转位)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- [LeetCode] 190. Reverse Bits 颠倒二进制位
Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 ...
- [LeetCode] 190. Reverse Bits 翻转二进制位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- Java for LeetCode 190 Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- LeetCode 190. Reverse Bits (算32次即可)
题目: 190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432 ...
随机推荐
- discuzx完全自定义设计模板门户首页,栏目,专题模板方法
第一种:门户首页模板(index.htm,保存于templatedefaultportal) <!--{subtemplate common/header}--> <style id ...
- explicit构造函数的作用
explicit构造函数是用来防止隐式转换的.请看下面的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...
- JDK中常见的package
SUN公司在JDK中为程序开发者提供了各种实用类,这些类按功能不同分别被放入了不同的包中,供开发者使用,下面简要介绍其中最常用的几个包:1. java.lang — 包含一些Java语言的核心类,如S ...
- 视频转换工具 Transmageddon
点这里 Transmageddon 是一个采用 Python 语言开发的视频转换工具,支持输出几乎所有的视频格式,同时也可以生成指定平台下的视频格式. 软件界面如下图所示
- hdu 3094 A tree game 博弈论
思路: 叶子节点的SG值为0:中间节点的SG值为它的所有子节点的SG值加1 后的异或和. 详见贾志豪神牛的论文:组合游戏略述 ——浅谈SG游戏的若干拓展及变形 代码如下: #include<cs ...
- Windows 代码实现关机(直接黑屏)
整理资料的时候发现的以前的代码,本机Win7 x64 Sp1 运行直接关机,黑屏.就是利用RtlAdjustPrivilege函数提权,代码中的注释写的很详细了.用的VS2010写的,直接编译成x64 ...
- http://jingyan.baidu.com/article/d169e186b38c37436611d8fa.html
http://jingyan.baidu.com/article/d169e186b38c37436611d8fa.html
- 李洪强iOS开发之多线程编程2-NSOperation
前言 1.上一讲简单介绍了NSThread的使用,虽然也可以实现多线程编程,但是需要我们去管理线程的生命周期,还要考虑线程同步.加锁问题,造成一些性能上的开销.我们也可以配合使用NSOperation ...
- jenkins创建git任务连接时遇到的问题
jenkins 创建任务后 配置 git时 报错 Jenkins Host key verification failed jenkins: Failed to connect to reposito ...
- DIV CSS设计时IE6、IE7、FF 与兼容性有关的特性(转载的)
在网站设计的时候,应该注意css样式兼容不同浏览器问题,特别是对完全使用DIV CSS设计的网,就应该更注意IE6 IE7 FF对CSS样式的兼容,不然,你的网乱可能出去不想出现的效果! 所有浏览器 ...