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. 0016SpringBoot实现RESTFUL形式的增删改查

    1.列表页面如下 <!DOCTYPE html><!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples ...

  2. 设置input 中placeholder的样式

    ::-webkit-input-placeholder { /* Chrome/Opera/Safari */ position: relative; top: 4px; } ::-moz-place ...

  3. 自定义的JSP标签

    JSP标签 JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能. JSTL支持通用的.结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签,SQL标签. 除了这 ...

  4. vue生命周期在工作中的用法

    1.首先来官方服生命=周期的解释: beforeCreate():实例在内存中被创建出来,还没有初始化好data和methods属性. create():实例已经在内存中创建,已经初始化好data和m ...

  5. PostgreSQL 配置参数

    一.配置参数所在文件.类型与查看方式 1.配置参数所在文件 postgresql.conf 2.配置参数类型 1)internal只读参数 这些参数不配置在postgresql.conf中,他们由po ...

  6. vue+上传文件夹

    在做项目开发的时候,上传东西无论文件也好,还是文件夹都需要用到 <input type="file" id="filepicker" name=" ...

  7. ZROI Day6比赛总结

    比赛还没结束而且我没有参加比赛就来这里了. T1 略 T2 设\(ans_d\)表示\(d|b_i\)的方案数(最后反演一下就可以) 设\(d\not|a_i\)的个数为\(l\)(可以\(O(n\l ...

  8. P3144 [USACO16OPEN]关闭农场——离线,并查集

    https://www.luogu.org/problem/P3144 每次关闭一个农场,农场之间有边相连,问每次关闭后开着的农场是否是一个连通块: 数据小,离线搞: 我们先记录删的顺序,然后倒着来, ...

  9. 初探nodejs事件循环机制event loop

    nodejs的特点 nodejs 具有事件驱动和非阻塞I/O的特点. 事件驱动是指nodejs把每一个任务当成事件来处理. 非阻塞I/O是指nodejs遇到I/O任务时,会从线程池调度单独的线程处理I ...

  10. CVE-2019-11477:Linux 内核中TCP协议栈整数溢出漏洞详细分析 代码卫士 今天

    CVE-2019-11477:Linux 内核中TCP协议栈整数溢出漏洞详细分析 代码卫士 今天