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 ...
随机推荐
- Nagle算法
简介 Nagle算法是以他的发明人John Nagle的名字命名的,它用于自动连接许多的小缓冲器消息:这一过程(称为nagling)通过减少必须发送包的个数来增加网络软件系统的效率.Nagle算法于1 ...
- 快速索引 (对View的自定义)
快速索引 (对View的自定义) 快速索引应用场景: 微信好友列表, 联系人通讯录, 应用管理, 文件管理等. 快速索引7步曲: *1. A-Z索引的绘制. * 2. 处理Touch事件. * 3. ...
- Spring之Core模块
Core模块主要的功能是实现了控制反转与依赖注入.Bean配置以及加载.Core模块中有Beans.BeanFactory.BeanDefinitions.ApplicationContext等概念 ...
- T-SQL 中的CROSS JOIN用法(半翻译)
突然发现个很吊的链接,我们来看看学习数据库要做些什么,胆小慎点:DBA工作内容!!!! 今天来翻译一篇关于T-SQL的文章,本文可供微软认证70-461:QueryingMicrosoft SQL S ...
- Android开发学习之路--UI之简单聊天界面
学了很多的ui的知识,这里就来实现个聊天的界面,首先来实现个layout的xml,代码如下: <?xml version="1.0" encoding="utf-8 ...
- SDL2源代码分析1:初始化(SDL_Init())
===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDL_Init()) SDL ...
- UNIX网络编程——TCP长连接与短连接的区别
一.TCP短连接 我们模拟一下TCP短连接的情况,client向server发起连接请求,server接到请求,然后双方建立连接.client向server发送消息,server回应client,然后 ...
- Libgdx 1.6.1发布,跨平台游戏开发框架
Libgdx 1.6.1发布 [1.6.1] 英文原文:http://www.badlogicgames.com/wordpress/?p=3694 译文翻译:宋志辉 - Net.newServerS ...
- 【翻译】从Store生成Checkbox Group
原文:Ext JS: Generating a Checkbox Group from a Store Ext JS的checkbox group可以用来将复选框组合成一个单一的逻辑字段.由于复选框时 ...
- C语言中标识符的作用域、命名空间、链接属性、生命周期、存储类型
Technorati 标签: C,标识符,作用域,命名空间,链接属性,生命周期,存储类型,scope,name space,linkage,storage durations,lifetime 无论学 ...