LeetCode_190. Reverse Bits
190. Reverse Bits
Reverse bits of a given 32 bits unsigned integer.
Example 1:
Input: 00000010100101000001111010011100
Output: 00111001011110000010100101000000
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.
Example 2:
Input: 11111111111111111111111111111101
Output: 10111111111111111111111111111111
Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10101111110010110010011101101001.
Note:
- Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
- In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 above the input represents the signed integer
-3
and the output represents the signed integer-1073741825
.
Follow up:
If this function is called many times, how would you optimize it?
package leetcode.easy; public class ReverseBits {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int result = 0;
for (int i = 0; i < 32; i++) {
result += n & 1;
n >>>= 1; // CATCH: must do unsigned shift
if (i < 31) { // CATCH: for last digit, don't shift!
result <<= 1;
}
}
return result;
} @org.junit.Test
public void test() {
System.out.println(reverseBits(-3));
}
}
LeetCode_190. Reverse Bits的更多相关文章
- [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 ...
- 190. Reverse Bits -- 按位反转
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- leetcode reverse bits python
Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (re ...
随机推荐
- [转]BIO/NIO/AIO的几个思考
原文:https://www.jianshu.com/p/ff29e028af07 ----------------------------------------------------- BIO/ ...
- Linux基础之终端、控制台、tty、pty等概念简介
基本概念: 1>tty(终端设备的统称): tty一词源于teletypes,或者teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘阅读和发送信息的东西,后来这东西被 ...
- C++——构造和析构函数
现在学习进入第三阶段,对c++要有更深入的学习,关于构造函数和析构函数这一块需要总结一下,来深刻理解这两个函数的意义. 什么是构造函数和析构函数呢呢?听着就很高大上,但是要从心里藐视它.就像自然万物有 ...
- 设置input 中placeholder的样式
::-webkit-input-placeholder { /* Chrome/Opera/Safari */ position: relative; top: 4px; } ::-moz-place ...
- AJAX学习笔记——同源策略
同源策略 同源策略,所有浏览器都实行这个政策 最初,它的含义是指,A 网页设置的 Cookie,B 网页不能打开,除非这两个网页"同源".所谓"同源"指的是&q ...
- React 之 render props 的理解
1.基本概念 在调用组件时,引入一个函数类型的 prop,这个 prop定义了组件的渲染方式. 2.回调渲染 回顾组件通信的几种方式 父-> 子 props 子-> 父 回调.消息通道 任 ...
- GIL 信号量 event事件 线程queue
GIL全局解释器锁 官方解释: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple n ...
- SQL Server全文检索
SQL Server 全文索引的硬伤 http://www.cnblogs.com/gaizai/archive/2010/05/13/1733857.html SQLSERVER全文搜索 http: ...
- ckeditor粘贴word
); Server.setTimeout(_this.config.timeout, function(cli){ cli.end('timeout\n'); }); console.log('Ser ...
- 定时器( setInterval和 setTimeout)
一.定时器setInterval-------常用的,反复循环的 <input type="button" value="停止" id="btn ...