• [LeetCode] 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 10111111111111111111111111111111.

    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?

    颠倒二进制位。

    题意是给一个数字,请你将其二进制的表达反转过来。

    思路是把input从右向左一位位的取出来或者说by digit一步步右移,如果取出来的是1,将结果 res 左移一位并且加上1;如果取出来的是0,将结果 res 左移一位即可。

    举个例子,如果要操作的部分是110的话

    第一轮

    首先res(0)往左移一位还是0,然后判断110的最低位 & 1 = 0,那么res还是0,此时n再右移一位,变成11。

    第二轮

    此时input是11,res再往左移一位得到00,此时1的最低位 & 1 = 1,那么res就变成01。

    第三轮

    此时input是1,res再往左移一位得到0010,此时最低位1 & 1 = 1,那么res就变成011

    时间O(1) - 因为无论如何也就只有32次操作

    空间O(1)

    Java实现

     1 public class Solution {
     2     // you need treat n as an unsigned value
     3     public int reverseBits(int n) {
     4         // corner case
     5         if (n == 0) {
     6             return 0;
     7         }
     8 
     9         // normal case
    10         int res = 0;
    11         for (int i = 0; i < 32; i++) {
    12             res <<= 1;
    13             if ((n & 1) == 1) {
    14                 res++;
    15             }
    16             n >>= 1;
    17         }
    18         return res;
    19     }
    20 }

    JavaScript实现

     1 /**
     2  * @param {number} n - a positive integer
     3  * @return {number} - a positive integer
     4  */
     5 var reverseBits = function(n) {
     6     if (n === 0) return 0;
     7     var res = 0;
     8     for (var i = 0; i < 32; i++) {
     9         res = res << 1;
    10         if ((n & 1) === 1) res++;
    11         n = n >> 1;
    12     }
    13     return res >>> 0;
    14 };

    LeetCode 题目总结

  • 相关阅读:
    项目发展规划 题解
    善意的投票&小M的作物 题解
    方格取数加强版 题解
    BZOJ1001 狼抓兔子 题解
    a
    一个搬运
    代码“小白”的温故而知新(一)-----OA管理系统
    工作流-----WorkFlow
    温习SQL语句
    浅谈MVC基础
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12154601.html
Copyright © 2020-2023  润新知