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

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

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

[LeetCode] 190. Reverse Bits 顛倒二進(jìn)制位

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 中有關(guān)位操作的題也有不少,比如 Repeated DNA Sequences,Single Number,   Single Number II ,和 Grey Code 等等。跟上面那些題比起來(lái),這道題簡(jiǎn)直不能再簡(jiǎn)單了,我們只需要把要翻轉(zhuǎn)的數(shù)從右向左一位位的取出來(lái),如果取出來(lái)的是1,將結(jié)果 res 左移一位并且加上1;如果取出來(lái)的是0,將結(jié)果 res 左移一位,然后將n右移一位即可,參見(jià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;
    }
};

我們可以簡(jiǎn)化上面的代碼,去掉 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ù)簡(jiǎn)化上面的解法,將 if 判斷句直接揉進(jìn)去,通過(guò) ‘或' 上一個(gè)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;
    }
};

博主還能進(jìn)一步簡(jiǎn)化,這里不更新n的值,而是直接將n右移i位,然后通過(guò) ‘與' 1來(lái)提取出該位,加到左移一位后的結(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;
    }
};

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

解法五:

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

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

相關(guān)文章

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

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

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

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

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

    C語(yǔ)言實(shí)現(xiàn)導(dǎo)航功能

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

    c語(yǔ)言實(shí)現(xiàn)詞頻統(tǒng)計(jì)的簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)?lái)一篇c語(yǔ)言實(shí)現(xiàn)詞頻統(tǒng)計(jì)的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • C++內(nèi)存分區(qū)模型超詳細(xì)講解

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

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

    C語(yǔ)言打印華氏-攝氏溫度對(duì)照表的方法

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

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

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

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

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

    C++ LeetCode300最長(zhǎng)遞增子序列

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

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

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

最新評(píng)論

通江县| 延吉市| 陇西县| 胶南市| 江都市| 新宾| 贺兰县| 汉川市| 会昌县| 镇雄县| 东乡族自治县| 穆棱市| 渑池县| 曲沃县| 南川市| 宝丰县| 河西区| 哈密市| 定陶县| 始兴县| 凤台县| 广元市| 庆元县| 鄂伦春自治旗| 五原县| 瑞丽市| 石台县| 青河县| 乡宁县| 乳山市| 柳林县| 孙吴县| 安义县| 志丹县| 梧州市| 石柱| 商都县| 乳山市| 剑川县| 柘荣县| 通山县|