#Leetcode# 190. Reverse Bits

Posted 丧心病狂工科女

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#Leetcode# 190. Reverse Bits相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/reverse-bits/

 

Reverse bits of a given 32 bits unsigned integer.

Example:

Input: 43261596
Output: 964176192
Explanation: 43261596 represented in binary as 00000010100101000001111010011100, 
             return 964176192 represented in binary as 00111001011110000010100101000000.

代码:

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        vector<int> ans;
        while(n) {
            ans.push_back(n % 2);
            n /= 2;
        }
        for(int i = ans.size(); i < 32; i ++)
            ans.push_back(0);
        int sum = 0;
        for(int i = 0; i <32; i ++)
            sum += ans[i] * (1 <<(31 - i));
        
        return sum;
    }
};

  Easy 杀手

以上是关于#Leetcode# 190. Reverse Bits的主要内容,如果未能解决你的问题,请参考以下文章

Java [Leetcode 190]Reverse Bits

Leetcode-190 Reverse Bits

LeetCode_190. Reverse Bits

[leetcode-190-Reverse Bits]

190. Reverse Bits(leetcode)

LeetCode 190 Reverse Bits