【LeetCode】190. Reverse Bits
题目:
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的更多相关文章
- 【LeetCode】190. Reverse Bits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...
- 【一天一道Leetcode】#190.Reverse Bits
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...
- 【刷题-LeetCode】190 Reverse Bits
Reverse Bits Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 0000001010010100000 ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【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 ...
- 【LeetCode】151. Reverse Words in a String
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...
- 【LeetCode】#7 Reverse Integer
[Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- 【leetcode❤python】 190. Reverse Bits
#-*- coding: UTF-8 -*- class Solution: # @param n, an integer # @return an integer def reve ...
随机推荐
- 回答集编程背景(Answer Set Programming)
毕业设计跟的导师是研究计算机理论的,花了三个月学习符号逻辑,试图优化一个回答集程序的求解器(Answer set solver).比起眼花缭乱的前端框架和热闹的社区讨论,符号逻辑就是一个挺小众的数学领 ...
- Redis学习-String
命令 描述 复杂的 返回值 SET key value [EX seconds] [PX milliseconds] [NX|XX] 将字符串值value关联到key.如果key已经持有其他值, ...
- mongodb新人扫盲
前言 数据库基本命令 集合(表)命令 增加数据 删除数据 更新数据 使用update()更新 使用save()命令实现upsert 自动更新信息 查询数据 mongoose的使用 前言 mongodb ...
- 如何升级php版本---从php5.5.12 升级php7.1.5 wamp实践
1.从官网下载一个php7.1.5 2.将刚下载的压缩包解压缩,修改命名为php7.1.5,即php+版本号. 3.将这个文件夹放在wamp/bin/php 目录下. 4.将原来版本的php5.5.1 ...
- JSP servlet的配置与使用
1. servlet 的配置文件内容如下所示 <servlet> <description>This is the description of my J2EE com ...
- [原创]Nexus5 内核编译烧录过程记录
参考Android系统源代码情况分析第二章进行实践,为了提高效率,也为了增加实践机会,使用Nexus5进行内核编译.需要说明的是,Android源代码工程默认是不包含它所使用的Linux内核源码,如果 ...
- springboot 1.5.2 thymeleaf 标签未关闭异常解决办法
org.thymeleaf.exceptions.TemplateInputException: Exception parsing document: template="login&qu ...
- 动态添加Redis密码认证
如果redis已在线上业务使用中,但没有添加密码认证,那么如何在不影响业务服务的前提下给redis添加密码认证,就是一个需要仔细考虑的问题. 本文描述一种可行的方案,适用于客户端使用了jedis连接池 ...
- 【FPGA】高斯白噪声的Verilog实现
本文章主要讨论高斯白噪声的FPGA实现.简单的方法可以采用在Matlab中产生服从一定均值和方差的I.Q两路噪声信号.然后将两组数据存在FPGA中进行回放,以此来产生高斯白噪声.这种方法优点是产生方法 ...
- WLAN高密无线网络部署的信道问题
WIFI信号的信道有两部分,其中2.4G频段有13个左右交叠的信道(14信道只在日本使用),其中只能找出3个相互不重合的信道(具体请参考文末的链接),最常用的就是1.6.11这三个,当然也可以使用其他 ...