【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 ...
随机推荐
- Java 通过先序后序序列生成二叉树
题目 二叉树的前序以及后续序列,以空格间隔每个元素,重构二叉树,最后输出二叉树的三种遍历方式的序列以验证. 输入: 1 2 3 4 5 6 7 8 9 10 3 2 5 4 1 7 8 6 10 9 ...
- JS执行效率与性能提升方案
如果是追加字符串,最好使用s+=anotherStr操作,而不是要使用s=s+anotherStr.如果要连接多个字符串,应该少使用+=,如 s+=a;s+=b;s+=c;应该写成s+=a + b + ...
- JAVA中LOCK
原文链接:http://www.cnblogs.com/dolphin0520/p/3923167.html 一.synchronized的缺陷 我们知道如果一个代码块被synchronized修饰了 ...
- C#单元测试,带你入门
注:本文示例环境 VS2017 XUnit 2.2.0 单元测试框架 xunit.runner.visualstudio 2.2.0 测试运行工具 Moq 4.7.10 模拟框架 为什么要编写单元测试 ...
- MHA在线切换的步骤及原理
在日常工作中,会碰到如下的场景,如mysql数据库升级,主服务器硬件升级等,这个时候就需要将写操作切换到另外一台服务器上,那么如何进行在线切换呢?同时,要求切换过程短,对业务的影响比较小. MHA就提 ...
- 《安卓网络编程》之第四篇 处理URL地址
在Android手机系统中,可以通过URL地址获取网络资源.在URL类的众多方法中,可以使用openStream()方法来读取该URL资源的输入流InputStream.在此方法的基础上可以引申出很多 ...
- 利刃 MVVMLight 10:Messenger 深入
1.Messager交互结构和消息类型 衔接上篇,Messeger是信使的意思,顾名思义,他的目是用于View和ViewModel 以及 ViewModel和ViewModel 之间的消息通知和接收. ...
- grok 匹配log4j
input { file { codec => multiline { pattern => "^\[2016" negate => true what => ...
- 网络编程应用:基于UDP协议【实现文件下载】--练习
要求: 基于UDP协议实现文件下载 发送方–请求–接收方发送文件–发送方接收文件 代码: 发送方: package Homework1; import java.io.File; import jav ...
- 使用Mingw编译wxSqlite3-3.0.5
最近在学习wxWidgets,而且官方也出了3.0版本,貌似还不错的样子,准备做个小程序来练手.中间需要用到数据库看到很多人推荐wxSqlite3就去下来看看,以下是我使用TDM-GCC 4.8.1( ...