题目:

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?

提示:

这道题比较简单,考的就是按位操作,可以借助于STL的bitset,也可以直接通过位运算完成题目的要求。

代码:

使用bitset:

class Solution {
public:
uint32_t reverseBits(uint32_t n) {
bitset<> bits(n);
int i = , j = , tmp;
for (; i < j; ++i, --j) {
tmp = bits[i];
bits[i] = bits[j];
bits[j] = tmp;
}
unsigned long l = bits.to_ulong();
return (uint32_t)l;
}
};

直接按位操作:

class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t m = ;
for (int i = ; i < ; ++i, n = n >> )
m = (m << ) + (n & );
return m;
}
}; 

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

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

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

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

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

  3. 【刷题-LeetCode】190 Reverse Bits

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

  4. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  5. 【LeetCode】190 & 191 - Reverse Bits & Number of 1 Bits

    190 - Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  6. 【LeetCode】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

  7. 【LeetCode】#7 Reverse Integer

    [Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...

  8. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  9. 【leetcode❤python】 190. Reverse Bits

    #-*- coding: UTF-8 -*- class Solution:    # @param n, an integer    # @return an integer    def reve ...

随机推荐

  1. 浅论Javascript在汽车信号测试中的应用

    起因 上周老板又给了我这个车辆工程毕业的码农一份工作: 要我写一个测试台架出来. 我先简单的分析了测试台架的几种典型的工况: 1.发送一个CAN信号,测试能否查到. 2.发送一个信号,是否能在规定时间 ...

  2. Haproxy------在windows下配置负载均衡

    配置Haproxy 1.解压Haproxy到d:\haproxy 2.置haproxy.cfg文件 global log 127.0.0.1 local0 maxconn 1500 daemon de ...

  3. RabbitMQ4--发后即忘和RPC

    在项目中引入RabbitMQ通常会考虑它会带来的好处:解耦应用程序,实现不同编程语言之间的互通,解除对特定通信协议的依赖,解除应用程序在时序上执行的依赖(异步).落实到代码层面就是两种常用应用模式:& ...

  4. BeanInstantiationException: Failed to instantiate [java.time.LocalDateTime]

    错误提示: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationExce ...

  5. js导出CSV

    <html> <head> <meta http-equiv="content-type" content="text/html; char ...

  6. JBoss7 如何用脚本 启动 和 停止

    用脚本来启动/停止JBoss服务器,有助于开发部署的 自动执行,提高工作效率. 在JBoss以前的版本中,很容易在bin目录下面找到 启动和停止服务器的脚本: run.bat shutdown.bat ...

  7. 对clear float 的理解

    之前自己对于清除浮动的用法比较模糊 ,如果用到的话,一般都是采用简单粗暴的方式解决,就是直接用overflow:hidden,但是越用久就会发现其实有BUG,这个BUG正是overflow:hidde ...

  8. RGB565的理解

    一个彩色图像由R G B三个分量组成,一个RGB565的每一个像素点数据为2Byte,即16位,那么从名字上就可看出来这16位中,高5位为R分量,中间6位为G分量,低5位为B分量. 下面做了一个实验, ...

  9. 常用统计分析 SQL 在 AWK 中的实现(转)

    转自:http://my.oschina.net/leejun2005/blog/100710 最近有需求需要本地处理一些临时的数据,用做统计分析.如果单纯的 MYSQL 也能实现, 不过一堆临时数据 ...

  10. springboot 获取hibernate 的 SessionFactory

    注入bean package cn.xiaojf; import cn.xiaojf.today.data.rdb.repository.RdbCommonRepositoryImpl; import ...