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. CSSOM之getComputedStyle,currentStyle,getPropertyValue,getAttribute

    js关于CSSOM编程的样式相关几个常用的方法 webkit:getComputedStyle,getPropertyValue IE:currentStyle,getAttribute 前言 jqu ...

  2. FileStream和StreamWriter配合写数据流时,出现数据写不进去的问题

    今天做一个数据导进txt小程序时,发现一个问题,就是没有关闭sw的流,只关闭了fs的,结果写不进去数据,看代码是对的,就是不行,最后就随手填上了sw的关闭后,可以了,反复测试,竟然就是这的问题,有点搞 ...

  3. Android ViewPager实现选项卡切换

    ViewPager实现选项卡切换,效果图如下: 步骤一:这里使用兼容低版本的v4包进行布局,这里eclipse没有输入提示,所以要手动输入,文件名称“activity_main.xml” <Re ...

  4. 五大权限:UGO权限、SetUID SetGID Sticky、ACL权限、chattr(文件系统级别的权限)、SELINUX

    五大权限:UGO权限.SetUID SetGID Sticky.ACL权限.chattr(文件系统级别的权限).SELINUX   ======================文件属性以及ugo权限= ...

  5. 【转载】CSS规范

    原文地址:http://www.cnblogs.com/whitewolf/p/4491707.html 目录 HTML 语法 HTML5 doctype 语言属性(Language attribut ...

  6. ASP.NET 配置KindEditor文本编辑器

    ASP.NET 配置KindEditor文本编辑器 跟着这篇博客做了两个小时,只搞出了下面这么个东西. 时间浪费在了原博客里这样的一句话:将kindeditor/asp.net/bin/LitJSON ...

  7. MySQL按照汉字的拼音排序,mysql汉字排序

    按照汉字的拼音排序,用的比较多是在人名的排序中,按照姓氏的拼音字母,从A到Z排序: 如果存储姓名的字段采用的是GBK字符集,那就好办了,因为GBK内码编码时本身就采用了拼音排序的方法(常用一级汉字37 ...

  8. 使用File类递归列出E盘下全部文件

    import java.io.File;public class FileListTest { public void tree(File file){ if(file.listFiles()!=nu ...

  9. python窗体——pyqt初体验

    连续两周留作业要写ftp的作业,从第一周就想实现一个窗体版本的,但是时间实在太短,qt零基础选手表示压力很大,幸好又延长了一周时间,所以也就有了今天这篇文章...只是为了介绍一些速成的方法,还有初学者 ...

  10. shell中&&和||的使用方法

    测试题: [ -z "" ] && echo 0 || echo 1 的结果是多少 看看这两个 && || 的用户  http://blog.csd ...