<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)461:漢明距離

          共 992字,需瀏覽 2分鐘

           ·

          2021-12-14 01:14

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

          今天和大家聊的問(wèn)題叫做?漢明距離,我們先來(lái)看題面:
          https://leetcode-cn.com/problems/hamming-distance/

          The Hamming distance between two integers is the number of positions at which the corresponding bits are different.


          Given two integers x and y, return the Hamming distance between them.

          兩個(gè)整數(shù)之間的 漢明距離 指的是這兩個(gè)數(shù)字對(duì)應(yīng)二進(jìn)制位不同的位置的數(shù)目。
          給你兩個(gè)整數(shù) x 和 y,計(jì)算并返回它們之間的漢明距離。

          示例? ? ? ? ? ? ? ? ? ? ? ? ? ? ?


          示例 1:
          輸入:x = 1, y = 4
          輸出:2
          解釋:
          1 (0 0 0 1)
          4 (0 1 0 0)
          ??↑? ↑
          上面的箭頭指出了對(duì)應(yīng)二進(jìn)制位不同的位置。

          示例 2:
          輸入:x = 3, y = 1
          輸出:1



          解題

          依次記錄兩個(gè)數(shù)字每位是否相同,如果不同,計(jì)數(shù)器加一。


          class?Solution?{
          public:
          ????int?hammingDistance(int?x, int?y)?{
          ????????int?count=0;
          ????????while(x||y)
          ????????{
          ????????????if(x%2!=y%2) ++count;
          ????????????x/=2;
          ????????????y/=2;
          ????????}
          ????????return?count;
          ????}
          };


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

          上期推文:

          LeetCode1-440題匯總,希望對(duì)你有點(diǎn)幫助!



          瀏覽 23
          點(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>
                  天天综合网永久 | 亚洲天堂资源站 | 香蕉伊人电影网站 | 大鸡吧久久久 | 日韩成人性爱视频 |