【一天一道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 ...
随机推荐
- [Baltic2004]sequence
题目描述: 给定一个序列t1,t2,...,tn ,求一个递增序列z1<z2<...<zn , 使得R=|t1−z1|+|t2−z2|+...+|tn−zn| 的值最小.本题中,我们 ...
- bzoj3224Tyvj 1728 普通平衡树 treap
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 17706 Solved: 7764[Submit][St ...
- bzoj1493[NOI2007]项链工厂 线段树
1493: [NOI2007]项链工厂 Time Limit: 30 Sec Memory Limit: 64 MBSubmit: 1712 Solved: 723[Submit][Status] ...
- 凸包(BZOJ1069)
顶点一定在凸包上,我们枚举对角线,观察到固定一个点后,随着另一个点的增加,剩下两个点的最优位置一定是单调的,于是就得到了一个优秀的O(n^2)做法. #include <cstdio> # ...
- C语言中#define的用法
今天整理了一些#define的用法,与大家共享! 1.简单的define定义 #define MAXTIME 1000 一个简单的MAXTIME就定义好了,它代表1000,如果在程序里面写 if(i& ...
- python3全栈开发-并发编程的多进程理论
一. 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): duoduo在一个时间段内有很多任务要做:python备课的任务,写 ...
- PAT甲级真题打卡:1001.A+B Format
题目: Calculate a + b and output the sum in standard format -- that is, the digits must be separated i ...
- 阿里Java研发工程师实习面经
十分幸运 拿到阿里云的offer,感谢周围无数人对我的支持和鼓励,所以写篇面经希望可以帮助大家. 面试中,运气占很大一部分的,所以你们若是没有通过,一定不要气馁,继续加油. 每个努力的人 都值得钦佩, ...
- 关闭默认共享,禁止ipc$空连接
关闭默认共享,禁止ipc$空连接 要防止别人用ipc$和默认共享入侵,需要禁止ipc$空连接,避免入侵者取得用户列表,并取消默认共享 禁止ipc$空连接进行枚举运行regedit,找到如下组键[HKE ...
- Mac 上Tomcat装载
I recently installed Tomcat 7 and got it working with Eclipse Helios on Mac OSX Lion.Install Homebre ...