最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實現(xiàn)LeetCode(190.顛倒二進制位)

 更新時間:2021年08月05日 14:29:36   作者:Grandyang  
這篇文章主要介紹了C++實現(xiàn)LeetCode(190.顛倒二進制位),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下

[LeetCode] 190. Reverse Bits 顛倒二進制位

Reverse bits of a given 32 bits unsigned integer.

Example 1:

Input: 00000010100101000001111010011100
Output: 00111001011110000010100101000000
Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000.

Example 2:

Input: 11111111111111111111111111111101
Output: 10111111111111111111111111111111
Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10101111110010110010011101101001.

Note:

  • Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
  • In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 above the input represents the signed integer -3 and the output represents the signed integer -1073741825.

Follow up:

If this function is called many times, how would you optimize it?

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

這道題又是在考察位操作 Bit Operation,LeetCode 中有關位操作的題也有不少,比如 Repeated DNA Sequences,Single Number,   Single Number II ,和 Grey Code 等等。跟上面那些題比起來,這道題簡直不能再簡單了,我們只需要把要翻轉(zhuǎn)的數(shù)從右向左一位位的取出來,如果取出來的是1,將結(jié)果 res 左移一位并且加上1;如果取出來的是0,將結(jié)果 res 左移一位,然后將n右移一位即可,參見代碼如下:

解法一:

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

我們可以簡化上面的代碼,去掉 if...else... 結(jié)構(gòu),可以結(jié)果 res 左移一位,然后再判斷n的最低位是否為1,是的話那么結(jié)果 res 加上1,然后將n右移一位即可,代碼如下:

解法二:

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

我們繼續(xù)簡化上面的解法,將 if 判斷句直接揉進去,通過 ‘或' 上一個n的最低位即可,用n ‘與' 1提取最低位,然后將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;
    }
};

博主還能進一步簡化,這里不更新n的值,而是直接將n右移i位,然后通過 ‘與' 1來提取出該位,加到左移一位后的結(jié)果 res 中即可,參加代碼如下:

解法四:

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

我們也可以換一種角度來做,首先將n右移i位,然后通過 ‘與' 1來提取出該位,然后將其左移 (32 - i) 位,然后 ‘或' 上結(jié)果 res,就是其顛倒后應該在的位置,參見代碼如下: 

解法五:

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

Github 同步地址:

https://github.com/grandyang/leetcode/issues/190

類似題目:

Number of 1 Bits

Reverse Integer

參考資料:

https://leetcode.com/problems/reverse-bits/

https://leetcode.com/problems/reverse-bits/discuss/54938/A-short-simple-Java-solution

https://leetcode.com/problems/reverse-bits/discuss/54772/The-concise-C++-solution(9ms)

https://leetcode.com/problems/reverse-bits/discuss/54741/O(1)-bit-operation-C++-solution-(8ms)

https://leetcode.com/problems/reverse-bits/discuss/54738/Sharing-my-2ms-Java-Solution-with-Explanation

https://leetcode.com/problems/reverse-bits/discuss/54873/Java-two-methods-using-String-or-bit-operation-6ms-and-2ms-easy-understand

到此這篇關于C++實現(xiàn)LeetCode(190.顛倒二進制位)的文章就介紹到這了,更多相關C++實現(xiàn)顛倒二進制位內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • C++成員函數(shù)中const的使用詳解

    C++成員函數(shù)中const的使用詳解

    這篇文章主要為大家詳細介紹了C++成員函數(shù)中const的使用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • C++的多態(tài)與虛函數(shù)你了解嗎

    C++的多態(tài)與虛函數(shù)你了解嗎

    這篇文章主要為大家詳細介紹了C++多態(tài)與虛函數(shù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • C語言實現(xiàn)導航功能

    C語言實現(xiàn)導航功能

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)導航功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • c語言實現(xiàn)詞頻統(tǒng)計的簡單實例

    c語言實現(xiàn)詞頻統(tǒng)計的簡單實例

    下面小編就為大家?guī)硪黄猚語言實現(xiàn)詞頻統(tǒng)計的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • C++內(nèi)存分區(qū)模型超詳細講解

    C++內(nèi)存分區(qū)模型超詳細講解

    在了解內(nèi)存分區(qū)之前,我們先來聊一聊為什么要進行內(nèi)存分區(qū)。在進行了內(nèi)存分區(qū)之后,在不同的區(qū)域存放的數(shù)據(jù),會有不同的生命周期,從而會讓程序員的編程變得更加靈活
    2022-11-11
  • C語言打印華氏-攝氏溫度對照表的方法

    C語言打印華氏-攝氏溫度對照表的方法

    這篇文章主要介紹了C語言打印華氏-攝氏溫度對照表的方法,涉及C語言字符串與數(shù)字操作的相關技巧,非常簡單實用,需要的朋友可以參考下
    2015-07-07
  • Qt快速讀取大文件最后一行內(nèi)容解決方案

    Qt快速讀取大文件最后一行內(nèi)容解決方案

    這篇文章主要給大家介紹了關于Qt如何快速讀取大文件最后一行內(nèi)容的解決方案,文中通過代碼介紹的非常詳細,對大家學習或者使用Qt具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-01-01
  • C++單例模式實現(xiàn)線程池的示例代碼

    C++單例模式實現(xiàn)線程池的示例代碼

    這篇文章主要為大家詳細介紹了如何利用C++單例模式簡單實現(xiàn)一個線程池,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-04-04
  • C++ LeetCode300最長遞增子序列

    C++ LeetCode300最長遞增子序列

    這篇文章主要為大家介紹了C++ LeetCode300最長遞增子序列示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • 全局靜態(tài)存儲區(qū)、堆區(qū)和棧區(qū)深入剖析

    全局靜態(tài)存儲區(qū)、堆區(qū)和棧區(qū)深入剖析

    在C++中,內(nèi)存可分為系統(tǒng)數(shù)據(jù)區(qū),自由存儲區(qū),文本區(qū),const數(shù)據(jù)區(qū),全局靜態(tài)區(qū),堆區(qū)和棧區(qū)
    2012-11-11

最新評論

神木县| 武夷山市| 墨竹工卡县| 绿春县| 丹巴县| 丹巴县| 谷城县| 阿鲁科尔沁旗| 永登县| 哈密市| 普陀区| 九寨沟县| 泾源县| 开江县| 泉州市| 海安县| 内乡县| 集安市| 长泰县| 吉木乃县| 马关县| 安陆市| 榕江县| 张家界市| 卓资县| 衡东县| 罗城| 鹿邑县| 木里| 叶城县| 博野县| 福州市| 临泉县| 罗田县| 龙南县| 庆云县| 赤壁市| 新河县| 布尔津县| 新安县| 铜鼓县|