leetcode_num190_Reverse Bits

Reverse bits of a given 32 bits unsigned integer.

归并法

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        n=(n>>16)|(n<<16);
        n=((n&0xff00ff00)>>8)|((n&0x00ff00ff)<<8);
        n=((n&0xf0f0f0f0)>>4)|((n&0x0f0f0f0f)<<4);
        n=((n&0xcccccccc)>>2)|((n&0x33333333)<<2);
        n=((n&0xaaaaaaaa)>>1)|((n&0x55555555)<<1);
        return n;
    }
};

>> << 移动补零

交替数字法:

class Solution {
public:
    uint32_t exchange(int i,int j,uint32_t m){
        int lo=(m>>i)&1;
        int hi=(m>>j)&1;
        if (lo!=hi){
            m^=((1<<i)|(1<<j));
        }
        return m;
    }

    uint32_t reverseBits(uint32_t n) {
        int L=32;
        for(int i=0;i<L/2;i++){
            n=exchange(i,L-1-i,n);
        }
        return n;
    }
};

性质:0异或x=x (x=0,1)

If this
function is called many times, how would you optimize it?

建立所有情况的对照表,直接根据表对应读出

时间: 2024-08-04 10:16:43

leetcode_num190_Reverse Bits的相关文章

[LeetCode]Reverse Bits

题目:Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through thi

191. 求1的位数 Number of 1 Bits

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should retu

【LeetCode】338. Counting Bits (2 solutions)

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

[LeetCode][Java][JavaScript]Counting Bits

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

Java [Leetcode 190]Reverse Bits

题目描述: everse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000). 解题思路: 移位操作. 代码如下: publi

leetcode笔记:Reverse Bits

一. 题目描述 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). 二. 题目分析 题目的要求比较简单,输

Codeforces Round #276 (Div. 1) A. Bits 贪心

A. Bits Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x. You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and is max

LeetCode OJ:Number of 1 Bits(比特1的位数)

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu

[LeetCode]Number of 1 Bits

Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the fun