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

Related problem: Reverse Integer

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

从高位开始check每一位n是0还是1,然后在低位对应位置设置相应0或者1.

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        unsigned ret = 0;
        unsigned pos1 = 1 << 31;
        unsigned pos2 = 1;
        while(pos1)
        {
            if(pos1 & n)
                ret |= pos2;
            pos2 <<= 1;
            pos1 >>= 1;
        }
        return ret;
    }
};

时间: 2024-12-20 23:18:16

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

【leetcode】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

【LeetCode】Evaluate Reverse Polish Notation

题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"]

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】190 - Triangle

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】Evaluate Reverse Polish Notation JAVA

   一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*&

【LeetCode】345. Reverse Vowels of a String 解题小结

题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 应该算不上有难度. class Solution { p

【LeetCode】007 Reverse Interger

题目:LeetCode 007 Reverse Interger 题意:将一个整数的数字反转.保留正负符号. 思路:先将整数变成字符串,然后判断是否为负数,或是否含有’+’,然后从字符串末尾开始累计得到新整数即可. 但是还会有特殊情况,即正向为Int范围内,但反转之后会溢出,因此要进行特判. 代码如下: 1 class Solution { 2 public: 3 int reverse(int x) { 4 int len, flag = 1, i = 0; 5 long long ans =

【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】25. Reverse Nodes in k-Group

题目描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, on