190 Reverse Bits 颠倒二进制位
颠倒给定的32位无符号整数的二进制位。
例如,给定输入 43261596(二进制表示为 00000010100101000001111010011100 ),返回 964176192(二进制表示为 00111001011110000010100101000000)。
问题进阶:
如果多次调用这个函数,你将如何优化它?
详见:https://leetcode.com/problems/reverse-bits/description/
Java实现:只需要把要翻转的数从右向左一位位的取出来,如果取出来的是1,将结果res左移一位并且加上1;如果取出来的是0,将结果res左移一位,然后将n右移一位即可
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int res=0;
for(int i=0;i<32;++i){
if((n&1)==1){
res=(res<<1)+1;
}else{
res=res<<1;
}
n=n>>1;
}
return res;
}
}
参考:https://www.cnblogs.com/grandyang/p/4321355.html
190 Reverse Bits 颠倒二进制位的更多相关文章
- [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 (算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. 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 ...
- 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 ...
- 190. Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- 【LeetCode】190. Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
随机推荐
- IE9版本号下面ajax 跨域问题解决
ajax跨域请求数据在谷歌火狐我本地IE11都是没问题的. 让測试就发现问题了,IE8下请求不到数据.然后我查看一下自己写的js看有没有不兼容问题.但是都没有啊.为什么就请求不到呢. 我把ajax的e ...
- 程序C++ to C#交互
第一次用C#调用C/C++生成的DLL文件,感觉有点新鲜,事实上仅仅是实现了执行在公共语言执行库 (CLR) 的控制之外的"非托管代码"(执行在公共语言执行库(CLR)的控制之中的 ...
- 【教程】怎样申请Chrome应用商店(Web Store)开发人员
首先你须要一张信用卡,假设你没有的话.能够借用父母或他人的(多见于学生党) 假设你有信用卡.你还得看看信用卡正面是否有注明"VISA"."MasterCard" ...
- 【PLSQL Developer】PLSQL Developer SQL Editor 乱码问题
[问题]我们常常在PLSQL Developer的SQL窗体编写各种语句.当须要保存这些语句时,能够另存为文本文件,也能够复制后粘贴到Word文件里.放在Word文件里的优点是语句保留原来的格式,能够 ...
- iOS与HTML交互问题
一. 加载后台传过来的HTML标签,文字都能正常显示但是图片显示不了.找问题找了很久没有发现那个地方写错,也问了别人都不知道,后来问了Android才知道,后台传过来的HTML标签,有些是转义过的.移 ...
- Spring Task 定时任务
所谓定时任务.就是依据我们设定的时间定时运行任务,就像定时发邮件一样,设定时间到了.邮件就会自己主动发送. 在Spring大行其道的今天,Spring也提供了其定时任务功能,Spring Task.同 ...
- FMDB中常用SQL使用
大家工作中,最常用到的无非是 增.删.查.改... 在SQL中对应的语句为:INSERT DELETE SELECT UPDATE 首先,你可以使用一款叫做“sqlite database brows ...
- 最简单的基于FFmpeg的移动端样例:IOS 视频转码器
===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...
- java操作CMD命令
import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; public class CM ...
- YTU 2800: 逗逗泡泡的保密电文
2800: 逗逗泡泡的保密电文 时间限制: 1 Sec 内存限制: 128 MB 提交: 229 解决: 114 题目描述 某电报局的电文保密的规律是将每个英文字母变成其后的第4个字母(很原始的那 ...