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?

思路

基本思路即分别提取出当前最高位和最低位的信息,进行交换即可,然后最高位置--,最低位位置++,直到相遇;32位数,所以0-15和16-31分别进行操作。这边我先判断最高位,如果为1,则判断最低位,如果也为1,不进行操作,如果为0,则最低为变成1,这边用到了位移,然后最高位做减法,则变为0。如果最高位为0,同理,做相反的操作即可。

class Solution {
public:
uint32_t reverseBits(uint32_t n) {
int cur;
for(int i = 31 ; i >= 16;i--){
if(n&(1<<i)){
cur = n&1<<(31-i);
if(cur == 0){
n -= 1<<i;
n += 1<<(31-i);
} }
else{
cur = n&1<<(31-i);
if(cur != 0){
n += 1<<i;
n -=1<<(31-i);
}
}
}
return n;
}
};

  

LeetCode 【190. Reverse Bits】的更多相关文章

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

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

  2. 【一天一道Leetcode】#190.Reverse Bits

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  3. 【LeetCode】190. Reverse Bits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...

  4. 【LeetCode】190. Reverse Bits

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

  5. 【刷题-LeetCode】190 Reverse Bits

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

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

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

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

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

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

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

  9. Java for LeetCode 190 Reverse Bits

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

随机推荐

  1. vs2010 vc++ 统一修改所有工程的目录配置

    vs2005和vs2008中都是通过 工具-选项-项目和解决方案-VC++目录,设置 头文件include .库文件lib.可执行文件dll的路径,以便在引用dll动态链接库文件时,可以查找到该文件的 ...

  2. IE6/7常用的hack

    hack基础: IE6: _selector{property:value;} selector{property:value;property:value !important;} //IE6 不支 ...

  3. CSS cursor属性

    介绍: 该属性规定要显示的光标的类型,该属性定义了鼠标指针放在一个元素边界范围之内的时候所用的光标的形状. 常用的属性值: default:默认光标 auto:浏览器默认的光标 pointer:光标呈 ...

  4. Android之获取string.xml文件里面的方法

    获取string.xml文件里面的方法 在此做个笔记: 1.在AndroidManifest.xml与layout等xml文件里: android:text="@string/resourc ...

  5. 带选择的sql简单用法

    一般写法: select * from itcast_topic order by (case type when 2 then 2 else 1 end ) desc ,postTime desc ...

  6. Centos7 搭建 Keepalived+LVS 备注

    NAT模型需要RealServer gateway设定为,DR模式需要执行 RealServer.sh.需要先安装network-tools. #!/bin/bash#description : st ...

  7. VRP

    VRP系统命令采用分级保护方式,命令被划分为参观级.监控级.配置级.管理级4个级别. 参观级:网络诊断工具命令(ping.tracert).从本设备出发访问外部设备的命令(包括:Telnet客户端.S ...

  8. progresql - 常用的管理命令

    1.查看当前数据库实例的版本 Select version(); 2.查看数据库的启动时间 Select pg_postmaster_start_time(); 3.查看最后load配置文件的时间 s ...

  9. ABAP之声母韵母

    我们一开始上学的时候,老师最先教的是什么? 拼音,声母,韵母,声调等等. 那么ABAP里什么是这些东西呢? 基础的数据类型,已经数据字典里的东西:域,数据元素,结构,视图,表,搜索帮助,锁... 数据 ...

  10. 老麦看点:SEO高手的两大秘诀

    一.技术真的是主导因素吗? 很多人站长朋友操作一段网站之后,发现自己的排名还是在渺渺无期,真可谓:“众里寻排名千百度,可是排名却不在阑珊处”,于是我们开始怀疑自己,怀疑自己的技术等,但是我们静下心里仔 ...