2016.5.16——leetcode:Reverse Bits(超详细讲解)
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&0xf0f0f0f0 = 1001 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&0xcccccccc = 11 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 00 <<左移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(超详细讲解)的更多相关文章
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- [leetcode] Reverse Bits
Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (re ...
- leetcode reverse bits python
Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (re ...
- LeetCode Reverse Bits 反置位值
题意:给定一个无符号32位整数,将其二进制形式左右反置,再以整型返回. 思路:循环32轮,将n往右挤出一位就补到ans的尾巴上. class Solution { public: uint32_t r ...
- [LeetCode] Reverse Bits 位操作
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- Keras代码超详细讲解LSTM实现细节
1.首先我们了解一下keras中的Embedding层:from keras.layers.embeddings import Embedding: Embedding参数如下: 输入尺寸:(batc ...
- 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 = ...
- freemaker超详细 讲解 配置
一.FreeMarker简介 二.第一个FreeMark示例 2.1.新建一个Maven项目 2.2.添加依赖 2.3.添加存放模板的文件夹 2.4.添加模板 2.5.解析模板 2.6.运行结果 三. ...
- 【Java面试题】15 String s="Hello"; s=s+“world!”;这两行代码执行后,原始的String对象中的内容到底变了没有?String与StringBuffer的超详细讲解!!!!!
1.Java中哪些类是不能被继承的? 不能被继承的是那些用final关键字修饰的类.一般比较基本的类型或防止扩展类无意间破坏原来方法的实现的类型都应该是final的,在java中,System,Str ...
随机推荐
- 【转载】css3动画简介以及动画库animate.css的使用
原文地址:http://www.cnblogs.com/2050/p/3409129.html 在这个年代,你要是不懂一点点css3的知识,你都不好意思说你是个美工.美你妹啊,请叫我前端工程师好不好. ...
- .net mvc C#生成网页快照
目标:调用某一网页,自动抓取整个页面为图片,并保存 public class WebSiteThumbnail { Bitmap m_Bitmap; string m_Url; public WebS ...
- POJ1149_PIGS
一共有n个猪圈,m个客人,一开始每个猪圈都有一定数量的猪猪.每个客人会打开一些猪圈,带走最多为某一个数量的猪猪,剩下的猪猪可以任意分配在这些开了的猪圈里面,然后重新关上.问所有的客人最多可以带走多少猪 ...
- Eclipse中设置作者、日期等的方式
1.点击Windows->Preferences->Java->Code Style->Code Templates: 2.点击展开右侧的Comments选项卡,里面的选项对应 ...
- 学习Spring Boot:(六) 集成Swagger2
前言 Swagger是用来描述和文档化RESTful API的一个项目.Swagger Spec是一套规范,定义了该如何去描述一个RESTful API.类似的项目还有RAML.API Bluepri ...
- docker attach 和 docker exec
docker attach docker attach -- Attach to a running container. 常用选项: --sig-proxy=true:Proxy all recei ...
- JS的类型和值
1.类型 ECMAScript语言中所有的值都有一个对应的语言类型.ECMAScript语言类型包括Undefined.Null.Boolean.String.Number和Object. 对语言引擎 ...
- bzoj 1539: [POI2005]Dwu-Double-row
假设一列交换表示为1,不换表示为0. 身高相同的两个人相当于给其中两列了一个限制条件,要么是两个必须相等,要么一个为零一个为一. 有了关系后我们就可以把每列当成一个点建边,边权为0表示必须相同,1为必 ...
- MVC中数据验证
http://www.studyofnet.com/news/339.html http://www.cnblogs.com/kissdodog/archive/2013/05/04/3060278. ...
- Selenium--使用参考
Selenium--浏览器上的按键精灵 Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同是Selenium 可 ...