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 is called many times, how would you optimize it?

思路

基本思路即分别提取出当前最高位和最低位的信息,进行交换即可,然后最高位--,最低位++,直到相遇;32位数,所以0-15和16-31分别进行操作。这边我先判断最高位,如果为1,则判断最低为,如果也为1,不进行操作,如果为0,则最低为变成1,这边用到了位移,然后最高位做减法,则变为0。如果最高位为0,则同理,做相反的操作即可。

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        int cur;
        for(int i = 31 ; i >= 16;i--){
            if(n&(1<<i)){
                cur = n&1<<(31-i);
                if(cur == 0){
                n -= 1<<i;
                n += 1<<(31-i);
                }

            }
            else{
                cur = n&1<<(31-i);
                if(cur != 0){
                n += 1<<i;
                n -=1<<(31-i);
                }
            }
        }
        return n;
    }
};

  

时间: 2024-07-29 12:03:41

LeetCode 【190. Reverse Bits】的相关文章

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

leetcode 【 Pascal&#39;s Triangle 】python 实现

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码:oj测试通过 Runtime: 46 ms 1 class Solution: 2 # @return a list of lists of integers 3 def generat

【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

[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

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 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