leetcode 190

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?

Related problem: Reverse Integer

按位翻转输入的整数。

代码如下:

 1 class Solution {
 2 public:
 3     uint32_t reverseBits(uint32_t n) {
 4         unsigned ret = 0;
 5         unsigned pos1 = 1 << 31;
 6         unsigned pos2 = 1;
 7         while(pos1)
 8         {
 9             if(pos1 & n)
10                 ret |= pos2;
11             pos2 <<= 1;
12             pos1 >>= 1;
13         }
14         return ret;
15     }
16 };
时间: 2024-10-18 13:03:40

leetcode 190的相关文章

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

[Leetcode]190. 颠倒二进制位

颠倒给定的 32 位无符号整数的二进制位. 示例: 输入: 43261596 输出: 964176192 解释: 43261596 的二进制表示形式为 00000010100101000001111010011100 , 返回 964176192,其二进制表示形式为 00111001011110000010100101000000 . 进阶:如果多次调用这个函数,你将如何优化你的算法? 方法1 class Solution { public: uint32_t reverseBits(uint3

leetcode 190. 颠倒二进制位(Reverse Bits)

目录 题目描述: 示例 1: 示例 2: 进阶: 解法: 题目描述: 颠倒给定的 32 位无符号整数的二进制位. 示例 1: 输入: 00000010100101000001111010011100 输出: 00111001011110000010100101000000 解释: 输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596, 因此返回 964176192,其二进制表示形式为 0011100101111000001010010