题目地址:https://leetcode.com/problems/reverse-bits/description/

题目描述

Reverse bits of a given 32 bits unsigned integer.

Example:

Input: 43261596
Output: 964176192
Explanation: 43261596 represented in binary as 00000010100101000001111010011100, 
             return 964176192 represented in binary as 00111001011110000010100101000000.

Follow up:

1、 Ifthisfunctioniscalledmanytimes,howwouldyouoptimizeit?;

解题方法

二进制字符串翻转

python真的好。bin()能求出二进制表示,ine(str,2)能把二进制转十进制表示。注意这个题目里面一定要是32位的二进制表示才行,因此一定要进行补全。

class Solution:
    @param n, an integer
    @return an integer
    def reverseBits(self, n):
        bins = bin(n)[2:][::-1]
        rev = (bins + '0' * (32 - len(bins)))
        return int(rev, 2)

1 2 3 4 5 6 7

位运算

二刷使用C++,进行位运算。每次看n的末尾数字是多少,然后拼接到res的最前面。这样循环32次,则把n的所有数字进行了逆序操作。

class Solution {
public:
    uint32_t reverseBits(uint32_t n) {
        uint32_t res = 0;
        for (int i = 0; i < 32; ++i) {
            res = (res << 1) + (n & 1);
            n >>= 1;
        }
        return res;
    }
};

1 2 3 4 5 6 7 8 9 10 11

DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有

本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发