【一天一道Leetcode】#190.Reverse Bits
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
我的个人博客已创建,欢迎大家持续关注!
一天一道leetcode系列依旧在csdn上继续更新,除此系列以外的文章均迁移至我的个人博客
另外,本系列文章已整理并上传至gitbook,网址:点我进
欢迎转载,转载请注明出处!
(一)题目
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).
(二)解题
题目大意:给定一个32位的无符整形数,将其按位反转。
解题思路:首先判断第i位上是否为1,如果为1,则表示反转后的数的31-i位上为1;如果为0,则跳过。
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t reBits = 0;
uint32_t tmp = 1;
uint32_t Bits = 2147483648;//Bits为10000000 00000000 00000000 00000000
for(int i = 0 ; i < 32 ; i++){
if((n&tmp)==tmp){//这一位上为1
reBits+=Bits>>i;//Bits右移i位
}
tmp = tmp<<1;
}
return reBits;
}
};
【一天一道Leetcode】#190.Reverse Bits的更多相关文章
- LeetCode 190. Reverse Bits (算32次即可)
题目: 190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432 ...
- [LeetCode] 190. Reverse Bits 颠倒二进制位
Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 ...
- LeetCode 190. Reverse Bits (反转位)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- [LeetCode] 190. Reverse Bits 翻转二进制位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- Java for LeetCode 190 Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- Leetcode 190. Reverse Bits(反转比特数)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- Java [Leetcode 190]Reverse Bits
题目描述: everse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...
- Leetcode 190 Reverse Bits 位运算
反转二进制 class Solution { public: uint32_t reverseBits(uint32_t n) { uint32_t ans = ; ; i<; ++i,n &g ...
- 【LeetCode】190. Reverse Bits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...
随机推荐
- 51 nod 1406 与查询
1406 与查询 题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有n个整数.输出他之中和x相与之后结果为x的有多少个.x从0 ...
- hdu5444(模拟)
题意:建树,给你几个点,要求输出走到各个点的路径(左为E,右为W,树的遍历) 二叉树的模拟题,但是GG了两次. 主要是没注意到直接模拟会爆掉- -,进行下处理就好了 #include <iost ...
- 例10-11 uva11181
题意:n个人去逛超市,第i个人买东西的概率是pi,,计算每个人实际买了东西的概率 思路: 搜索标处理所以的情况,得出所有概率和all(开始天真的以为是1 - -,就说怎么案例看着怪怪的),用tt[i] ...
- Java8的重要新特性
一.Lambda表达式 java8中Lambda表达式的书写方式: (参数) -> 表达式 (参数) -> 单行语句 (参数) -> { 语句 } 1.Lambda遍历List和Ma ...
- SQL 收缩数据库日志的几种办法 (2005与2008 略有区别)
在SQL Server 2000/2005中可以快速压缩日志log文件,通过SQL, 方法一: ---DBTEST 为数据库名,顺序最好别乱.注意:要先截断再清空,最后收缩! backup log D ...
- VMWare 学习目录
Linux介绍 Linux入门--个人感想 Google怎么用linux 初入Linux Windows XP硬盘安装Ubuntu 12.04双系统图文详解 实例讲解虚拟机3种网络模式(桥接.nat. ...
- Axis2 webservice入门--Webservice的发布与调用
一.Webservice发布 参考 http://www.cnblogs.com/demingblog/p/3263576.html 二.webservice 调用 部分参考:http://www.c ...
- linux route 路由设置小记
情景一: 有一台ip为172.16.160.53服务器,此服务器为固定ip,由于某些特殊情况,此服务器的ip不能修改. 现在这台服务器需要与另外一个网段ip为172.16.176.150服务器进行局域 ...
- LINUX逻辑卷(LVM)管理与逻辑卷分区
LINUX之逻辑卷管理与逻辑卷扩展 LVM是逻辑卷管理(Logical Volume Manager)的简称,他是建立在物理存储设备之上的一个抽象层,允许你生成逻辑存储卷,和直接使用物理存储在管理上相 ...
- Java不走弯路教程(1.环境搭建)
1.环境搭建在开始写第一个Java程序之前,我们需要做一些简单的准备工作. 1.1 还记得DOS吗 我们可以通过图形界面来操作我们的电脑.但作为程序员,你首先需要学会用命令行的方式来操作电脑,因为不是 ...