zoukankan      html  css  js  c++  java
  • leetcode| 190. 颠倒二进制位

    ##颠倒给定的 32 位无符号整数的二进制位。 示例 1: 输入: 00000010100101000001111010011100 输出: 00111001011110000010100101000000 解释: 输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596, 因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000。

    示例 2:
    输入:11111111111111111111111111111101
    输出:10111111111111111111111111111111
    解释:输入的二进制串 11111111111111111111111111111101 表示无符号整数 4294967293,
    因此返回 3221225471 其二进制表示形式为 10101111110010110010011101101001。

    思路

    时间复杂度O(n),空间复杂度O(1)。

    代码

    public class Solution {
        // you need treat n as an unsigned value
        public int reverseBits(int n) {
            int ans = 0;
            for(int i = 0; i < 32; i++) {
                int temp = (n>>i & 1);
                ans |= temp << (31 - i);
            }
            return ans;
        }
    }
    

    链接:https://leetcode-cn.com/problems/reverse-bits

  • 相关阅读:
    LR(0)分析法
    算符优先法之优先表构造
    自上而下的LL(1)语法分析法
    K倍区间
    全排列
    mysql自动获取时间日期
    限制
    JQuery
    LinQ 组合查询与分页
    LinQ 简单使用
  • 原文地址:https://www.cnblogs.com/ustca/p/12307699.html
Copyright © 2011-2022 走看看