[LeetCode] Reverse Bits

简介: 1Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as

1Reverse 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?

解题思路1

做法与字符串转化为整形类似,只不过这里是二进制,而且是从右边开始处理。

实现代码1

//Runtime:10 ms
#include <iostream>
#include "inttypes.h"
using namespace std;

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        uint32_t result = 0;
        int i = 32;
        while(i--)
        {
            result = result * 2 + (n & 0x1);
            n >>= 1;
        }

        return result;
    }
};

int main()
{
    Solution s;
    cout<<s.reverseBits(43261596)<<endl;
    return 0;
}

解题思路2

在思路1的基础上解除对机器上uint32_t长度的依赖

实现代码2

//Runtime:9 ms
#include <iostream>
#include "inttypes.h"
using namespace std;
class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        uint32_t result = 0;
        uint32_t i;
        for(i = 1; i != 0; i <<= 1)
        {
            result <<= 1;
            result |= (n & 0x1);
            n >>= 1;
        }

        return result;
    }
};

解题思路3

通过直接对无符号整型数字的位进行交换来解题

实现代码3

//Runtime:10 ms
class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        uint32_t i;
        uint32_t x = sizeof(x) * 8; //uint32_t的位数
        for(i = 0; i < x / 2; i++)
        {
            swapBit(n, i, x - i - 1);
        }

        return n;
    }

    void swapBit(uint32_t& n, uint32_t i, uint32_t j){
        uint32_t lo = (n >> i) & 0x1; 
        uint32_t hi = (n >> j) & 0x1;
        if (lo ^ hi) //第i位和第j位不相等
        {
            //0 ^ 1 = 1
            //1 ^ 1 = 0,从而达到交换目的
            n ^= ((1U << i) | (1U << j));
        }
    }
};
目录
相关文章
|
机器学习/深度学习 NoSQL 算法
LeetCode 344. 反转字符串 Reverse String
LeetCode 344. 反转字符串 Reverse String
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 206. 反转链表 Reverse Linked List
|
机器学习/深度学习 NoSQL
LeetCode 344. Reverse String
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
70 0
LeetCode 344. Reverse String
|
算法 C++
LeetCode 338. Counting Bits
给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。
54 0
LeetCode 338. Counting Bits
LeetCode 190. Reverse Bits
颠倒给定的 32 位无符号整数的二进制位。
62 0
LeetCode 190. Reverse Bits
【LeetCode】Counting Bits(338)
  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.
82 0
LeetCode之Reverse String II
LeetCode之Reverse String II
84 0
LeetCode之Reverse String
LeetCode之Reverse String
70 0
LeetCode之Reverse Integer
LeetCode之Reverse Integer
77 0
LeetCode 150:逆波兰表达式求值 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. 说明: 整数除法只保留整数部分。
1354 0