<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)190:顛倒二進(jìn)制位

          共 1942字,需瀏覽 4分鐘

           ·

          2021-02-21 14:02

          算法的重要性,我就不多說(shuō)了吧,想去大廠,就必須要經(jīng)過(guò)基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問(wèn)題叫做?顛倒二進(jìn)制位,我們先來(lái)看題面:
          https://leetcode-cn.com/problems/reverse-bits/

          Reverse bits of a given 32 bits unsigned integer.

          題意


          顛倒給定的 32 位無(wú)符號(hào)整數(shù)的二進(jìn)制位。

          示例


          示例 1:

          輸入: 00000010100101000001111010011100
          輸出: 00111001011110000010100101000000
          解釋: 輸入的二進(jìn)制串 00000010100101000001111010011100 表示無(wú)符號(hào)整數(shù) 43261596,
          ?????因此返回 964176192,其二進(jìn)制表示形式為 00111001011110000010100101000000。

          示例 2:

          輸入:11111111111111111111111111111101
          輸出:10111111111111111111111111111111
          解釋:輸入的二進(jìn)制串 11111111111111111111111111111101 表示無(wú)符號(hào)整數(shù) 4294967293,
          ? 因此返回 3221225471 其二進(jìn)制表示形式為 10111111111111111111111111111111 。



          解題


          JDK 自帶的 Integer.reverse() 方法源碼

          /**
          ?????* Returns the value obtained by reversing the order of the bits in the
          ?????* two's complement binary representation of the specified {@code?int}
          ?????* value.
          ?????*
          ?????* @param?i the value to be reversed
          ?????* @return?the value obtained by reversing order of the bits in the
          ?????* specified {@code?int} value.
          ?????* @since?1.5
          ?????*/

          ????public?static?int?reverse(int?i)?{
          ????????// HD, Figure 7-1
          ????????i = (i & 0x55555555) << 1?| (i >>> 1) & 0x55555555;
          ????????i = (i & 0x33333333) << 2?| (i >>> 2) & 0x33333333;
          ????????i = (i & 0x0f0f0f0f) << 4?| (i >>> 4) & 0x0f0f0f0f;

          ????????return?reverseBytes(i);
          ????}


          我們每次都拿出來(lái)n的最后一個(gè)數(shù)字,然后再左移去把這些數(shù)字加起來(lái)。

          public?class?Solution?{
          ????// you need treat n as an unsigned value
          ????public?int?reverseBits(int?n)?{
          ????????int?res = 0;
          ????????for?(int?i = 0; i < 32; i++) {
          ????????????res = (res << 1) + (n & 1);
          ????????????n >>= 1;
          ????????}
          ????????return?res;
          ????}
          }



          好了,今天的文章就到這里,如果覺(jué)得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

          上期推文:

          LeetCode1-180題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)181:超過(guò)經(jīng)理收入的員工
          LeetCode刷題實(shí)戰(zhàn)182:查找重復(fù)的電子郵箱
          LeetCode刷題實(shí)戰(zhàn)183:從不訂購(gòu)的客戶
          LeetCode刷題實(shí)戰(zhàn)184:部門工資最高的員工
          LeetCode刷題實(shí)戰(zhàn)185:部門工資前三高的所有員工
          LeetCode刷題實(shí)戰(zhàn)186:翻轉(zhuǎn)字符串里的單詞 II
          LeetCode刷題實(shí)戰(zhàn)187:重復(fù)的DNA序列
          LeetCode刷題實(shí)戰(zhàn)188:買賣股票的最佳時(shí)機(jī) IV
          LeetCode刷題實(shí)戰(zhàn)189:旋轉(zhuǎn)數(shù)組

          瀏覽 58
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人精品在线视频 | 亚洲视屏在线免费观看 | 天天好逼网综合 | 天天松天天干天天操天天添 | 高清无码在线视频网站 |