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的更多相关文章

  1. LeetCode OJ:Reverse Bits(旋转bit位)

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

  2. LeetCode 192:Reverse Bits

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

  3. LeetCode 190. Reverse Bits (反转位)

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

  4. [LeetCode] 190. Reverse Bits 颠倒二进制位

    Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 ...

  5. [LeetCode] 190. Reverse Bits 翻转二进制位

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

  6. 【leetcode】Reverse Bits(middle)

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

  7. Java for LeetCode 190 Reverse Bits

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

  8. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  9. LeetCode 190. Reverse Bits (算32次即可)

    题目: 190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432 ...

随机推荐

  1. ajax 乱码

    1. 在页面的中文变量前添加encodeURIComponent() $.ajax({ type: "POST", url: "", data:{   id:e ...

  2. C#[Serializable]在C#中的作用-NET 中的对象序列化

    为什么要使用序列化?最重要的两个原因是:将对象的状态保存在存储媒体中以便可以在以后重新创建出完全相同的副本:按值将对象从一个应用程序域发送至另一个应用程序域.例如,序列化可用于在 ASP.NET 中保 ...

  3. jQuery scroll事件

    scroll事件适用于window对象,但也可滚动iframe框架与CSS overflow属性设置为scroll的元素. $(document).ready(function () { //本人习惯 ...

  4. javascript利用拷贝的方法实现导出excel(可以导出表格线)

    Js代码: <script language=javascript> function preview() { window.clipboardData.setData("Tex ...

  5. C# 调用Windows API实现两个进程间的通信

    使用Windows API实现两个进程间(含窗体)的通信http://blog.csdn.net/huangxinfeng/article/details/5513608 从C#下使用WM_COPYD ...

  6. (转)android ListView详解

    转自:  http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html 在android开发中ListView是比较常用的组件,它以列表的形 ...

  7. Linux问题定位工具大放送

    我们在程序定位问题时,经常不知所错,但是在linux有很多强大的工具,只要我们合理利用,一定见奇效. 主要会遇到以下问题: 1 mem高 2 cpu高 3 io高 4 网络延迟高 vargrind:h ...

  8. 【转】terminal 快捷键

    转自:http://www.jb51.net/os/Ubuntu/141723.html 1.gnome-terminal快捷键设置方法: 系统 —> 首选项 ->键盘快捷键 -> ...

  9. HTTP返回码总结

    HTTP协议状态码表示的意思主要分为五类,大体是:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~  1×× 保留  2×× 表示请求成功地接收  3×× 为完成请求客户需进一步细化请求  ...

  10. PHP页面跳转几种实现技巧

    PHP被许多程序员用来开发WEB的首选语言.在实际开发中,网站的各项功能都可以通过PHP语言的编写来满足,比如PHP页面跳转这一方法. 探讨PHP变量解析顺序如何获取提交数据 深入解读PHP运行机制 ...