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. 根据多年经验整理的《互联网MySQL开发规范》

    一.基础规范 使用 INNODB 存储引擎 表字符集使用 UTF8  所有表都需要添加注释 单表数据量建议控制在 5000W 以内 不在数据库中存储图⽚.文件等大数据 禁止在线上做数据库压力测试 禁⽌ ...

  2. Linux shell中单引号,双引号及不加引号的简单区别

    简要总结: 单引号: 可以说是所见即所得:即将单引号内的内容原样输出,或者描述为单引号里面看见的是什么就会输出什么. 双引号: 把双引号内的内容输出出来:如果内容中有命令,变量等,会先把变量,命令解析 ...

  3. Pycharm 介绍

    Pycharm官方站点: http://www.jetbrains.com/pycharm/ Pycharm百科: Pycharm百度百科 Pycharm是由jetbrains开发的优秀的python ...

  4. 多版本Python管理及Python连接MySQL

    Python有个非常别扭的地方,就是两个不兼容的版本,很尴尬,有的包只能在低版本的2.7上才能运行,比如即将用到的MySQLdb. 所以首先必须在系统上安装两个版本的Python(貌似在pycharm ...

  5. Android性能优化篇

    很多App都会遇到以下几个常见的性能问题: 启动速度慢:界面跳转慢:事件响应慢:滑动和动画卡顿. 一.启动速度优化. 优化初始化任务: 1. 把一些初始化任务懒加载初始化 2. 把初始化任务并行化(异 ...

  6. Google API在线生成二维码的方法

    1.先看一个实例,是用Google API生成西部e网的网站地址www.weste.net二维码的方法: http://chart.apis.google.com/chart?cht=qr&c ...

  7. HR函数学习02——分配组织单位

    REPORT ZLYHR01. "创建组织单元 成功 DATA:LS_OBJ TYPE OBJEC, LV_STU TYPE GDSTR-SVECT, LV_TIT TYPE CHAR20, ...

  8. iOS基础篇(十七)——UIGestureRecognizer用法

    UIGestureRecognizer(手势识别)在iOS 中非常重要,他极大地提高了移动设备的使用便捷性: 在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)t ...

  9. 一些常用的方法(1)--去除DataTable中的重复数据

    private DataTable Display(DataTable dtSource)        {            DataTable dtTemp = dtSource.Copy() ...

  10. iOS流量监控

    http://code4app.com/snippets/one/iOS%E6%B5%81%E9%87%8F%E7%9B%91%E6%8E%A7/5020ba7a6803fae325000000 1. ...