OJ练习37——T190 Reverse Bits

把一个32位无符号整数按位翻转,

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).

【思路】

可以首尾位交换,使用位运算和移位运算。

但是有个棘手的地方,想要把低位变成不能使用&(高位移位),因为如果高位是1,低位是0,结果就是0,不等于高位;

如果使用|(高位移位),又会有高位是0,低位是1,结果还是不等于高位。所以不能单单使用位运算。

【my code】

uint32_t reverseBits(uint32_t n) {
        uint32_t temp1,temp2,low,high,dis;
        high=0x80000000;
        low=0x1;
        dis=31;
        while(low<high){
            temp1=high & n;
            temp2=low & n;
            if(temp1!=temp2){
            if(temp2&&!temp1){
                n+=high;
                n-=low;
            }
            else if(!temp2&&temp1){
                n+=low;
                n-=high;
            }
            }
            high>>=1;
            low<<=1;
            dis--;
        }
        return n;
    }

【反思】

对于位运算一直都很不熟练,对于位的交换纠结了很久。看起来不是个聪明的做法。

耗时9ms,排名在c的效率范围。

【other code】

uint32_t reverseBits(uint32_t n) {
    uint32_t answer;
    uint32_t i;

    answer = 0;

    /*把一个unsigned int 数字1一直左移,直到它变成全0的时候,也就得到了该机器内unsigned int的长度*/
    for (i = 1; i != 0; i <<= 1)
    {
        answer <<= 1;
        if (n & 1) { answer |= 1; }
        n >>= 1;
    }

    return answer;
    }

【评价】

该法“新开了空间”,不过也只是一个无符号整数大小,微不足道,人家代码是很简洁的。

妙处在于for循环中,i!=0的条件,i左移,直到溢出为0.这就遍历了要求的位数(这里倒是不必要,已知32)。

耗时10ms,排名和我的算法一样。

所以说,总是有更优秀的算法。

时间: 2024-08-11 05:35:58

OJ练习37——T190 Reverse Bits的相关文章

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

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]Reverse Bits

题目:Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through thi

leetcode笔记: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). 二. 题目分析 题目的要求比较简单,输

*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 funct

LeetCode 192: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 Reverse Bits 反置位值

题意:给定一个无符号32位整数,将其二进制形式左右反置,再以整型返回. 思路:循环32轮,将n往右挤出一位就补到ans的尾巴上. 1 class Solution { 2 public: 3 uint32_t reverseBits(uint32_t n) { 4 if( !n ) return 0; 5 uint32_t ans = 0; 6 int i; 7 for(i=0; i<32; i++ ) 8 { 9 ans <<= 1; 10 if( n & 1 ) 11 ans

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

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