lc面试准备:Reverse Bits
1 题目
Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).
Follow up:
If this function is called many times, how would you optimize it?
Related problem: Reverse Integer
接口
public int reverseBits(int n)
uint32_t reverseBits(uint32_t n)
2 思路
简单写一下,Java的思路。Java中是没有无符号整数的,只有有符号的int(0x80000000 ~ 0x7fffffff)。
&和|操作的结合使用。
复杂度
3 代码
public int reverseBits(int n) {
int res = 0;
for (int i = 0; i < 32; i++) {
int bit = (n >> i) & 1;
res |= bit << (31 - i);
}
return res;
}
4 总结
(n >> i) & 1是取余数的好方法,不用借助额外的空间。
5 参考
lc面试准备:Reverse Bits的更多相关文章
- lc面试准备:Number of 1 Bits
1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- 190. Reverse Bits 二进制相反数
[抄题]: Reverse bits of a given 32 bits unsigned integer. Example: Input: 43261596 Output: 964176192 E ...
- [LeetCode] Reverse Bits 翻转位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- Leetcode-190 Reverse Bits
#190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...
- 【leetcode】Reverse Bits(middle)
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 ...
- [leetcode] Reverse Bits
Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (re ...
- 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 ...
随机推荐
- 关于json中对象的删除
一个json对象在后台产生了,但是有些数据可能无效或者不合法,所以需要在前台作些例外处理,比如删除掉. json的删除有很多种,直接用过 delete json对象方式. 举例如下 Js代码 var ...
- 在iis中mantisbt配置过程
最近需要安装个mantisbt,由于不想再安装个apache服务器,因此直接使用iis作为php解析服务器.同时为了方便管理安装包,将php安装包和扩展包能够独立存放在D:\Program Files ...
- ASP.NET MVC 第七回 UrlHelper
这节讲 一下ASP.NET MVC中的Helper. 何谓Helper,其实就是在View中为了实现一些灵活功能而写的方法组. 其实ASP.NET MVC的View是Aspx的页面,本身可以声明定义方 ...
- [DEncrypt] Encrypt--加密/解密/MD5加密 (转载)
点击下载 Encrypt.zip 这个类是关于加密,解密的操作,文件的一些高级操作1.Encrypt加密2.Encrypt解密3.Encrypt MD5加密看下面代码吧 /// <summar ...
- [Mime] 在c#程序中放音乐的帮助类 (转载)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.M ...
- 《你不常用的c#之一》:略谈unsafe
转自csdn:http://blog.csdn.net/robingaoxb/article/details/6199508 msdn里讲到: “在 C# 中很少需要使用指针,但仍有一些需要使用的情况 ...
- Handler 原理分析和使用(一)
我为什么写Handler,原因主要还在于它在整个 Android 应用层面非常之关键,他是线程间相互通信的主要手段.最为常用的是其他线程通过Handler向主线程发送消息,更新主线程UI. 下面是一个 ...
- Windows环境下使用cygwin ndk_r9c编译x264
一.废话 最近学习,第一步就是编译.我们需要编译FFmpag,x264,fdk_aac,下面是x264,网上说的很多都是几百年前的,我亲测完美可用 还是那句话 我能力有限,但是我希望我写的东西能够让 ...
- oraclesql日志
select * from v$logfile; select * from v$sql select sql_text,module,action,parsing_schema_name,firs ...
- iOS消息推送机制
iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...