Leetcode_190_Reverse Bits
), return 964176192 (represented in binary as00111001011110000010100101000000).
思路:
(1)题意为给定无符号32位整数,求该整数翻转(对应的二进制数的翻转)后所得的整数值。
(2)该题主要考察位运算。由于限制位数为32位,所以只需对待处理的整数n进行32次右移位,每当低位&1的结果为1,说明低位为1,此时将待输出的目标整数(默认值为0)左移动一位并加上1;每当低位&1的结果为0,说明低位为0,此时将待输出的目标整数左移一位即可;循环直到移动完32次,所得目标整数即为所求。
(3)详情见下方代码。希望本文对你有所帮助。
算法代码实现如下:
/**
*
* @author lqq
*
*/
public class Reverse_Bits {
public int reverseBits(int n) {
int value = 0;
// 32位无符号数
for (int i = 0; i < 32; ++i) {
if ((n & 1) == 1) {
value = (value << 1) + 1; // 左移动
n >>= 1;
} else {
value = value << 1;
n >>= 1; // 右移
}
}
return value;
}
}
Leetcode_190_Reverse Bits的更多相关文章
- [LeetCode] Number of 1 Bits 位1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- Leetcode-190 Reverse Bits
#190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...
- CodeForces 485C Bits[贪心 二进制]
C. Bits time limit per test1 second memory limit per test256 megabytes inputstandard input outputsta ...
- uva12545 Bits Equalizer
uva12545 Bits Equalizer You are given two non-empty strings S and T of equal lengths. S contains the ...
- LeetCode Counting Bits
原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...
- Number of 1 Bits(Difficulty: Easy)
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- 解剖SQLSERVER 第五篇 OrcaMDF里读取Bits类型数据(译)
解剖SQLSERVER 第五篇 OrcaMDF里读取Bits类型数据(译) http://improve.dk/reading-bits-in-orcamdf/ Bits类型的存储跟SQLSERVE ...
随机推荐
- JAVA面向对象-----访问修饰符
访问修饰符是用来控制类.属性.方法的可见性的关键字称之为访问修饰符. 1.public 一个类中,同一包中,子类中,不同包中 2.protected 一个类中,同一包中,子类中 3.default 一 ...
- RxJava操作符(04-过滤操作)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51656494 本文出自:[openXu的博客] 目录: Debounce Distinct ...
- SQL语言四大类
SQL语言四大类 SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML,数据定义语言DDL,数据控制语言DCL. 数据查询语言DQL 数据查询语言DQL基本结构是由SELECT子句, ...
- Android 读取清单文件<meta-data>元素的数据
添加属性 <application -- > <meta-data android:value="Channel_0" android:name="UM ...
- 剑指offer面试题5 从头到尾打印链表(c)
- 带你深入理解STL之List容器
上一篇博客中介绍的vector和数组类似,它拥有一段连续的内存空间,并且起始地址不变,很好的支持了随机存取,但由于是连续空间,所以在中间进行插入.删除等操作时都造成了内存块的拷贝和移动,另外在内存空间 ...
- 最详细的制作正式版10.11 OS X El Capitan 安装U盘的方法
原帖地址:http://bbs.feng.com/read-htm-tid-10036487.html 一.准备工作: 1.准备一个 8GB 或以上容量的 U 盘,确保里面的数据已经妥善备份好(该过程 ...
- Java基础---Java---IO流-----对象的序列化、管道流、RandomAccessFile、数据类型的流对象DataStream、ByteArrayStream
ObjectInputStream 对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化. ObjectOutputStream 和 ObjectInputStream ...
- 【Android应用开发】 推送原理解析 极光推送使用详解 (零基础精通推送)
作者 : octopus_truth 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/45046283 推送技术产生场景 : -- ...
- Java异步通信
服务器端: import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; impo ...