190. Reverse Bits

Easy

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的更多相关文章

  1. [LeetCode] Reverse Bits 翻转位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  2. Leetcode-190 Reverse Bits

    #190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432615 ...

  3. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  4. LeetCode 【190. Reverse Bits】

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  5. [leetcode] Reverse Bits

    Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (re ...

  6. Reverse Bits

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  7. Java for LeetCode 190 Reverse Bits

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  8. 190. Reverse Bits -- 按位反转

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  9. leetcode reverse bits python

    Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (re ...

随机推荐

  1. P2280 [HNOI2003]激光炸弹[前缀和]

    题目描述 输入输出格式 输入格式: 输入文件名为input.txt 输入文件的第一行为正整数n和正整数R,接下来的n行每行有3个正整数,分别表示 xi,yi ,vi . 输出格式: 输出文件名为out ...

  2. Kafka 基本知识分享

    目录 一.基本术语 二.Kafka 基本命令 三.易混淆概念 四.Kafka的特性 五.Kafka的使用场景 六.Kakfa 的设计思想 七.Kafka 配置文件设置 八.新消费者 九.Kafka该怎 ...

  3. javascript常用工具类util.js

    //如果大家想要补充,请留言 /** * 判断指定名称的复选框是否被选中 * * @param {} * chname复选框名称 */ function chkCheckCha(chname) { v ...

  4. MySQL之三张表关联

    创建三张表 1.学生表 mysql> create table students( sid int primary key auto_increment, sname ) not null, a ...

  5. sublime 分屏显示 不是插件

    点击 view--layout --- 选择几屏即可(single / columns 2 ....) 快捷键  Alt + Shift + 1/2/3/4  分别对应1 ,2,3,4屏 如何把一个文 ...

  6. 如何自定义 GNOME 3 桌面?

    作者: Magesh Maruthamuthu 译者: LCTT 郑 | 2019-08-22 00:02   评论: 2 收藏: 1 我们收到很多来自用户的电子邮件,要我们写一篇关于 GNOME 3 ...

  7. ICPC 2019-2020 North-Western Russia Regional Contest

    目录 Contest Info Solutions Problem A. Accurate Movement Problem B. Bad Treap Problem E. Equidistant P ...

  8. P3802 小魔女帕琪 期望

    P3802 小魔女帕琪 期望 题面 题意稍微不清楚,题中的a[i]指的是属性i的魔法有a[i]个. 题目大意:有7种魔法,每种数量a[i],每次随机放出一个魔法,问放完为止出现7次魔法都不相同的期望次 ...

  9. Codeforces Round #525 (Div. 2)题解

    Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...

  10. python 列表 【基本使用功能】

    #!/usr/bin/python # -*- coding: UTF-8 -*- # by Mercury_Lc list1 = list # 开个新的列表的方法 list2 = [] list1 ...