题目:

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. 使用JsonConfig控制JSON lib序列化

    将对象转换成字符串,是非常常用的功能,尤其在WEB应用中,使用 JSON lib 能够便捷地完成这项工作.JSON lib能够将Java对象转成json格式的字符串,也可以将Java对象转换成xml格 ...

  2. 【charger battery 充電 充電器 電池】停充的種類

    Precondition : 配有 power path 功能的 BQ2589 手機. 接上 pc usb port. Origin : 今天有同事問我, 手機是否可以在接上 pc usb port ...

  3. kairosdb + cassandra Setup

    安装cassandra 下载 cassandra cassandra download mirror wget http://mirror.bit.edu.cn/apache/cassandra/2. ...

  4. TCP三次握手与四次分手

    TCP简介 首先来看看OSI的七层模型: 我们需要知道TCP工作在网络OSI的七层模型中的第四层--Transport层,IP在第三层--Network层,ARP在第二层--Data Link层:在第 ...

  5. RabbitMQ安装记录(CentOS)

    参照官方文档:http://www.rabbitmq.com/install-rpm.html Install Erlang from EPEL 激活EPEL源: rpm -ivh http://dl ...

  6. 【小练习06】HTML+CSS--电影公告

    要求实现如下效果图: 代码演示 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...

  7. 网络编程应用:基于UDP协议【实现文件下载】--练习

    要求: 基于UDP协议实现文件下载 发送方–请求–接收方发送文件–发送方接收文件 代码: 发送方: package Homework1; import java.io.File; import jav ...

  8. Chrome 开发者工具断点调试(视频教程)

    很多人不了解 Chrome Dev Tools (开发者工具)的使用方法和技巧. 其中很多技能,无论是前端开发从业者,还是普通用户,了解一些还是对日常很有帮助的. 本猿定期录制.甚至根据您的需求来订制 ...

  9. mysql too many connections 问题

    我的处理步骤: 第一步:首次确定你的服务可不可以重启,如果可以重启转第二步,如果不可以重启转第三步,这个主要考虑已经部署到客户现场或者正在使用中的数据库不能重启. 第二步:查找mysql的安装路径,这 ...

  10. css样式,边界和边框,格式和布局

    1.大小:width:宽:heigh:高 2.背景:1)background-color:背景颜色 2)background-image:背景图片url路径 3)background-repeat:图 ...