leetcode-190-Reverse Bits

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 as00111001011110000010100101000000).

Follow up:

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

将一个数的二进制反过来,求反过来的数。

期,将该数的最低位,作为最高位即可(2^31),然后依次求解。

例:

对于 0000..........10

ans = 0

ans += 0*(2^31)

ans += 1*(2^30)

...

...

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        uint32_t i = 1 ,ans = 0,m = 31; // 要用uint32_t 不能用int
        while (m --) { //  将 i = 2^31
            i <<= 1;
        }

        m = 32;
        while (n) {  // m --     用m-- 和n 都可以
            ans += (n&1)*i;
            i >>= 1;
            n >>= 1;
        }
        return ans;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-13 18:18:34

leetcode-190-Reverse Bits的相关文章

[LeetCode] 190. 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). Follow up:If this function

Leetcode 190. 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). Follow up: If this function

Java for LeetCode 190 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). JAVA实现如下: public int revers

LeetCode 190. 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). 题目标签: Bit Manipulation 这道题目

leetcode——190 Reverse Bits(32位无符号二进制数的翻转)

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). Follow up: If this function

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 190 Reverse Bits 位运算

反转二进制 1 class Solution { 2 public: 3 uint32_t reverseBits(uint32_t n) { 4 uint32_t ans = 0; 5 for (int i = 0; i<32; ++i,n >>=1){ 6 ans = (ans<< 1)|(n & 1); 7 } 8 return ans; 9 } 10 };

Leetcode solution 190: Reverse Bits

Problem Statement Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 Output: 00111001011110000010100101000000 Explanation: The input binary string 00000010100101000001111010011100 represents the unsig

190. Reverse Bits(leetcode)

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). Follow up:If this function

【LeetCode】190. 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 as00111001011110000010100101000000). Follow up:If this function i