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?

这个问题就是一个数字移位和赋值的问题,每次取出源操作数的最低位赋给目标操作数,并且源操作数右移一位,目标操作数左移一位。

  1. class Solution {
  2. public:
  3. uint32_t reverseBits(uint32_t n)
  4. {
  5. uint32_t iRet = 0;
  6. uint32_t iRes = n;
  7.  
  8. for(int i = 0; i != 32; ++i)
  9. {
  10. iRet <<= 1;
  11. iRet = iRet | iRes & 1;
  12. iRes >>= 1;
  13. }
  14. return iRet;
  15. }
  16. };

  学习了c++的移位运算,c++还是弱的不行,慢慢学,不放弃,总会有改变的!!!

LeetCode 192:Reverse Bits的更多相关文章

  1. LeetCode OJ:Reverse Bits(旋转bit位)

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

  2. 【LeetCode】190. Reverse Bits

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

  3. Leetcode 344:Reverse String 反转字符串(python、java)

    Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...

  4. leetcode:Reverse Bits

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

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

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

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

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

  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 Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 0000001010010100000 ...

  9. LeetCode算法题-Reverse Bits(Java实现)

    这是悦乐书的第185次更新,第187篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第44题(顺位题号是190).给定32位无符号整数,求它的反转位.例如: 输入:4326 ...

随机推荐

  1. 【题解搬运】PAT_A1016 Phone Bills

    从我原来的博客上搬运.原先blog作废. 题目 A long-distance telephone company charges its customers by the following rul ...

  2. CS229 1

    1.机器学习 机器学习是工具,具体应用到某个实际场景下,才是目的. 2.分类 a 监督学习,包括回归(regression),分类(classification).回归问题,数据可以是连续或者离散,分 ...

  3. Qt 实现脉搏检测-2,简陋的功能产品

    今天终于可以接上硬件来显示真是的脉搏情况了,上图 主要就是显示脉搏的心跳曲线,和IBI 数据来源是三个,串口,网口和蓝牙,目前只实现了串口,过程应该都是差不多的,监听,读取,解析,等硬件更新后,再次更 ...

  4. Wordpress 设置后台自定义post 排序

    创建新的 Post type时,文章在后台默认使用 Titile 列进行升序排序,但是通常情况下我们需要按日期 Date 进行降序排序, function wpse_81939_post_types_ ...

  5. 剑指offer-反转链表15

    题目描述 输入一个链表,反转链表后,输出新链表的表头. class Solution: # 返回ListNode def ReverseList(self, pHead): # write code ...

  6. Bellman_ford标准算法

    Bellman_ford求最短路可以说这个算法在某些地方和dijkstra还是有些相似的,它们的松弛操作基本还是一样的只不过dijkstra以图中每个点为松弛点对其相连接的所有边进行松弛操作 而Bel ...

  7. Python 随笔01---列表

    列表 1.取出列表中的元素方法?? 2.删除列表中的元素del.remove对比?? 3.linux 常用操作命令

  8. lintcode-52-下一个排列

    52-下一个排列 给定一个整数数组来表示排列,找出其之后的一个排列. 注意事项 排列中可能包含重复的整数 样例 给出排列[1,3,2,3],其下一个排列是[1,3,3,2] 给出排列[4,3,2,1] ...

  9. 编译程序提示配置PKG_CONFIG_PATH

    http://blog.csdn.net/langeldep/article/details/6804331 在安装开源软件的过程中, 经常会碰到提示配置PKG_CONFIG_PATH路径, 或者直接 ...

  10. Codeforces Round #518 Div. 1没翻车记

    A:设f[i][j][0/1]为前i个数第i位为j且第i位未满足/已满足限制的方案数.大力dp前缀和优化即可. #include<iostream> #include<cstdio& ...