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 ...
随机推荐
- java Reference(摘录)
Java中的Reference对象和GC是紧密联系在一起的,Reference的实现也是和GC相关的. 强引用 强引用是Java中使用最普遍的引用,我们经常使用的Object o = new Obje ...
- 使用 git 进行项目管理(只管理代码,不管理项目配置)
使用Git进行项目管理 1. 从服务器pull项目,本地还原工程 从服务器拉取仓库及分支 git clone git@github.com/helloWorld.git git branch -a g ...
- php错误处理和异常处理
PHP错误处理有两种:标准的错误处理和异常(OOP语法新出现的错误处理机制)
- Java SE (1)之 JFrame 组件 BorderLayout 布局
JAVA 初期,练习SE ,桌面程序, package com.sunzhiyan; import java.awt.*; import java.awt.event.*; import javax. ...
- Android - This Handler class should be static or leaks might occur.
今天学习了使用 HTTP协议,从Android客户端往Tomcat服务器端以GET发送请求,途中无意中发现自己写的Handler类被Android提示:This Handler class shoul ...
- jQuery Ajax(load,post,get,ajax)用法与详解
今天看到群里面有网友们问到Jquery Ajax的(load,post,get,ajax)之间的区别,现在整理了一篇文章出来,希望可以帮到网友们,首先我们先来看一些简单的方法, 这些方法都是对jQue ...
- CSS Outlines
轮廓(outline)是绘制于元素周围的一条线,位于边框边缘的外围,可起到突出元素的作用. 轮廓(outline)属性指定了样式,颜色和外边框的宽度. 轮廓(outline)实例 在元素周围画线本例演 ...
- Java之字符串学习
java中String的使用十分频繁,是我们要学习的重点,在说String之前,我们要知道堆跟栈的区别. java中的数据类型分原生数据类型(primitived types)有八种(byte,cha ...
- css 兼容 position:fixed
我是头 我是主体 有多少内容,我就有多高 我是脚 我要随滚动条滚动 我要随滚动条滚动 我要随滚动条滚动 <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...
- Bad owner or permissions on .ssh/config
Bad owner or permissions on $HOME/.ssh/config The ssh with RHEL 4 is a lot more anal about security ...