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   as 00111001011110000010100101000000).

  如果看不大懂题目可以这样来看:

  43261596:  00000010100101000001111010011100

  964176192:00111001011110000010100101000000

  这样就能看出来了,将整型转化的二进制数反转,得出反转后的二进制数。

  思路:

  我的思路:没有任何思路

  leetcode/discuss思路:

      思路1:新建一个uint32_t的数r: a:  r <<= 1; r每次左移,则原二进制数的最低位就变成新数的最高位了

                     b: r |= n & 1; 如果原数值为1则为1,为0则为0 (可否看做字符串,不可以!!因为输入的为整型数

                     c: n >>= 1;  原数值每次右移1位

      思路2:将原数值32位,先分为16位反转,16位再分为8位反转,8位再分为4位反转,4位分为2位反转,2位再分为1位反转。

  代码1:思路1的代码,比较简洁易懂的,但是复杂度为n

 uint32_t reverseBits(uint32_t n) {  //uint32_t位4个字节(4*8=32),int根据操作系统不同,字节数不同,16位操作为2字节,32位操作系统为4字节,64位为8字节
2 uint32_t ret=;
for(int i=; i<; i++) { //为什么没有考虑将十进制转换为二进制?? 因为用到& | << >>操作后,计算机就默认将整型转化成二进制。
ret<<=;
if(n&) {
ret|=;
}
n>>=;
}
return ret;
}

  代码2:思路二代码,复杂度为1.非常简洁,非常漂亮,膜拜大神的代码。

class Solution {
public:
uint32_t reverseBits(uint32_t n) {
n = (n >> ) | (n << ); //将32位分为16位反转,输入并不是二进制??如何右移
n = ((n & 0xff00ff00) >> ) | ((n & 0x00ff00ff) << );   //将16位的2分为8位反转、移位
n = ((n & 0xf0f0f0f0) >> ) | ((n & 0x0f0f0f0f) << ); //&分为4位反转、移位
n = ((n & 0xcccccccc) >> ) | ((n & 0x33333333) << ); //&分为2位反转、移位
n = ((n & 0xaaaaaaaa) >> ) | ((n & 0x55555555) << ); //&分为1位反转、移位
return n;
}
};

  0xff00ff00:11111111000000001111111100000000           0x00ff00ff:00000000111111110000000011111111

  【只有第一个8位和第三个8位不变,其他为0,右移到第二个8位和第四个8位】  【只有第二个8位和第四个8位不变,其他为0,右移到第一个8位和第三个8位】所以右移8位前两个8位相互交换位置,后两个8位会互调位置。

   0xf0f0f0f0:11110000111100001111000011110000        0x0f0f0f0f:00001111000011110000111100001111

   8位中的两两4位在交换位置

   0xcccccccc:11001100110011001100110011001100       0x33333333:00110011001100110011001100110011

   4位中的两两2位交换为值

  0xaaaaaaaa:10101010101010101010101010101010       0x55555555:01010101010101010101010101010101

  2位中的两两1位交换位置

  例如:如题目中的

  原32位(以16位分开):0000001010010100  0001111010011100

  右移16位a:          00011110 10011100 00000010 10010100  

  &运算右移8位:a&0xff00ff00 = 00011110 00000000 00000010 00000000 >>右移8位: 00000000 00011110 00000000 00000010

            a&0x00ff00ff = 00000000 10011100 00000000 10010100 <<左移8位: 10011100 00000000 10010100 00000000

                                        做 | (或)运算后b: 1001 1100 0001 1110 1001 0100 0000 0010

  &运算右移4位:b&0xf0f0f0f01001 0000 0001 0000 1001 0000 0000 0000 >>右移4位: 0000 1001 0000 0001 0000 1001 0000 0000

          b&0x0f0f0f0f = 0000 1100 0000 1110 0000 0100 0000 0010 <<左移4位: 1100 0000 1110 0000 0100 0000 0010 0000

                                         做 | (或)运算后c: 1100 1001 1110 0001 0100 1001 0010 0000

  &运算右移2位: c&0xcccccccc11 00 10 00 11 00 00 00 01 00 10 00 00 00 00 00 >>右移4位: 00 11 00 10 00 11 00 00 00 01 00 10 00 00 00 00

        c&0x33333333 = 00 00 00 01 00 10 00 01 00 00 00 01 00 10 00 00 <<左移4位: 00 00 01 00 10 00 01 00 00 00 01 00 10 00 00 00

                                       做 | (或)运算后d: 00 11 01 10 10 11 01 00 00 01 01 10 10 00 00 00

  &运算右移2位:d&0xaaaaaaaa = 00 10 00 10 10 10 00 00 00 00 00 10 10 00 00 00 >>右移2位: 00 01 00 01 01 01 00 00 00 00 00 01 01 00 00 00

        d&0x55555555 = 00 01 01 00 00 01 01 00 00 01 01 00 00 00 00 0<<左移4位: 00 10 10 00 00 10 10 00 00 10 10 00 00 00 00 00

                                        做 | (或)运算后e: 00 11 10 01 01 11 10 00 00 10 10 01 01 00 00 00

                                      这是题目中的反转后的值:00 11 10 01 01 11 10 00 00 10 10 01 01 00 00 00

  我的代码:带有main函数及 测试问题

 #include "stdafx.h"
#include "stdint.h" //uint32_t的头文件
#include "iostream"
using namespace std; class MyClass
{
public:
int ReverseBits(uint32_t n)
{
uint32_t res = ;
for (int i = ; i < ; i++) //这个i<32,而不是i<n
{
//cout << i << endl; //出错测试
res <<= ;
//cout << res << endl;
if (n & )
{
res |= ;
}
n >>= ;
//cout << n << endl;
}
return res;
}
}; class Solution
{
public:
int reverseBits(uint32_t n)
{
n = (n >> | n << );
n = ((n & 0xff00ff00) >> | (n & 0x00ff00ff) << );
n = ((n & 0xf0f0f0f0) >> | (n & 0x0f0f0f0f) << );
n = ((n & 0xcccccccc) >> | (n & 0x33333333) << );
n = ((n & 0xaaaaaaaa) >> | (n & 0x55555555) << ); return n;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
MyClass solution1;
//Solution solution2;
int m = ;
int nums = ;
cin >> nums;
m = solution1.ReverseBits(nums);
//m = solution2.reverseBits(nums);
cout << m << endl;
system("pause"); //”erro:字符常量中字符过多“ 字符中应该用双引号(“”)而不是单引号(‘’)
return ;
}

  错误:”erro:字符常量中字符过多“   原因:字符中应该用双引号(“”)而不是单引号(‘’)

  错误:结果值不对  原因:第12行:这个i<32,而不是i<n

2016.5.16——leetcode:Reverse Bits(超详细讲解)的更多相关文章

  1. [LeetCode] Reverse Bits 翻转位

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

  2. [leetcode] Reverse Bits

    Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (re ...

  3. leetcode reverse bits python

    Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (re ...

  4. LeetCode Reverse Bits 反置位值

    题意:给定一个无符号32位整数,将其二进制形式左右反置,再以整型返回. 思路:循环32轮,将n往右挤出一位就补到ans的尾巴上. class Solution { public: uint32_t r ...

  5. [LeetCode] Reverse Bits 位操作

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

  6. Keras代码超详细讲解LSTM实现细节

    1.首先我们了解一下keras中的Embedding层:from keras.layers.embeddings import Embedding: Embedding参数如下: 输入尺寸:(batc ...

  7. 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

    Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...

  8. freemaker超详细 讲解 配置

    一.FreeMarker简介 二.第一个FreeMark示例 2.1.新建一个Maven项目 2.2.添加依赖 2.3.添加存放模板的文件夹 2.4.添加模板 2.5.解析模板 2.6.运行结果 三. ...

  9. 【Java面试题】15 String s="Hello"; s=s+“world!”;这两行代码执行后,原始的String对象中的内容到底变了没有?String与StringBuffer的超详细讲解!!!!!

    1.Java中哪些类是不能被继承的? 不能被继承的是那些用final关键字修饰的类.一般比较基本的类型或防止扩展类无意间破坏原来方法的实现的类型都应该是final的,在java中,System,Str ...

随机推荐

  1. 给表格控件DBGrid加上记录序号的列

    DBGrid使用起来还是很方便的,但就是没有显示记录序号的功能,必须自己加,参照老外给的解决方案如下: 方案1: 1- 在DBGrid建一个第一列 (列的名字起“NO”) 2- 在DBGrid事件 D ...

  2. JVM中各种变量保存位置

    Java中变量分为静态变量,实例变量,临时变量.那么各种变量具体保存在JVM中的何处呢? 1 静态变量:位于方法区. 2 实例变量:作为对象的一部分,保存在堆中. 3 临时变量:保存于栈中,栈随线程的 ...

  3. Nginx web服务优化 (一)

    1.Nginx基本安全优化 a.更改配置文件参数隐藏版本 编辑nginx.conf配置文件增加参数,实现隐藏Nginx版本号的方式如下.在nginx配置文件nginx.conf中的http标签段内加入 ...

  4. 【hdu2809】 不要62

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 (题目链接) 题意 求区间${[n,m]}$中不含有62和4的数的个数. Solution 数位dp板子. ...

  5. Moonraker:1靶机入侵

      0x01 前言 攻击Moonraker系统并且找出存在最大的威胁漏洞,通过最大威胁漏洞攻击目标靶机系统并进行提权获取系统中root目录下的flag信息. Moonraker: 1镜像下载地址: h ...

  6. OS X 安装pyspider

    pyspider安装的过程中,需要安装pycurl.有几个坑 一.首先遇到权限的问题 因为/Library目录是root权限,所以非root用户对该目录的读写经常会遇到权限问题,但是不宜切换成root ...

  7. JavaScript演示下Singleton设计模式

    单例模式的基本结构: MyNamespace.Singleton = function() { return {}; }(); 比如: MyNamespace.Singleton = (functio ...

  8. 目标检测应用化之web页面(YOLO、SSD等)

    在caffe源码目录下的examples下面有个web_demo演示代码,其使用python搭建了Flask web服务器进行ImageNet图像分类的演示. 首先安装python的依赖库:pip i ...

  9. Could not update Activiti database schema: unknown version from database: '5.20.0.1'

    转: Could not update Activiti database schema: unknown version from database: '5.20.0.1' 2017年11月22日 ...

  10. gdb调试3_显示变量 和 数组

    http://www.cnblogs.com/shipfi/archive/2008/08/04/1260293.html  感谢作者! 程序变量查看文件中某变量的值:file::variablefu ...