LeetCode OJ 190题:Reverse Bits

题目分析:常规解法,实在不知道如何优化了。

 1 class Solution {
 2 public:
 3     uint32_t reverseBits(uint32_t n)
 4     {
 5         uint32_t m = 0;
 6         for (uint32_t i = 1; i != 0; i <<= 1)
 7         {
 8             m <<= 1;
 9             if (n & 1)
10             {
11                 m |= 1;
12             }
13             n >>= 1;
14         }
15
16         return m;
17     }
18 };

for循环中i!=0我认为用的比较巧妙,用这个方法你是可以测试uint32_t的位数的。当然本题你是可以把uint32_t当32位使用的,如果题目没告诉实参为32位整数,你必须要测试实参的实际位数。

时间: 2024-10-18 02:19:05

LeetCode OJ 190题:Reverse Bits的相关文章

leetcode_190题——Reverse Bits(bitset的使用)

Reverse Bits Total Accepted: 18472 Total Submissions: 66483My Submissions Question Solution Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176

LeetCode算法题-Reverse Bits(Java实现)

这是悦乐书的第185次更新,第187篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第44题(顺位题号是190).给定32位无符号整数,求它的反转位.例如: 输入:43261596 输出:964176192 说明:43261596以二进制表示为00000010100101000001111010011100, 964176192以二进制表示为00111001011110000010100101000000. 本次解题使用的开发工具是eclipse,jdk使用的版本是1

leetcode第七题--Reverse Integer

Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 终于什么都没参考就一次Accept了.可能是这题比较简单,同时自己也进步了一点点,leetcode就是这样给我们增加信心的吧. 我是这样想的,利用除10和模10两个操作,将每个数字分离在vec中,然后相应的乘以10的倍数输出就行.如果这个数在-10与10之间直接返回就好. 代码如下: class S

leetcode第24题--Reverse Nodes in k-Group

problem: 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,

LeetCode OJ 133题:Clone Graph

题目挺长的,其实只需要关注第一行就OK了.这道题思路挺明显的,对于图来说要么BFS,要么DFS,至于具体细节,我觉得与138题:Copy List with Random Pointer很像.在BFS或DFS过程中,可能要调整顶点的邻接点,这个时候不要忘了它的邻接点可能还没有创建.所以,有以下思路: 遍历两遍无向图.第一遍,创建所有结点,不用保存顶点间关系.第二遍,调整顶点间关系.顶点间关系保存在neighbors中,为了能够找到新创建顶点的位置,第一遍遍历时候需要保存原顶点与新顶点指针的一一对

LeetCode—Reverse Bits ,1 Bit和数字的二进制情况相关

https://leetcode.com/problems/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 as0011100101111000001

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