1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實(shí)戰(zhàn)397:整數(shù)替換

        共 2012字,需瀏覽 5分鐘

         ·

        2021-10-01 23:50

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

        今天和大家聊的問題叫做 整數(shù)替換,我們先來看題面:
        https://leetcode-cn.com/problems/integer-replacement/

        Given a positive integer n, you can apply one of the following operations:


        If n is even, replace n with n / 2.

        If n is odd, replace n with either n + 1 or n - 1.

        Return the minimum number of operations needed for n to become 1.


        示例

        給定一個(gè)正整數(shù) n ,你可以做如下操作:

        如果 n 是偶數(shù),則用 n / 2替換 n 。
        如果 n 是奇數(shù),則可以用 n + 1或n - 1替換 n 。

        n 變?yōu)?1 所需的最小替換次數(shù)是多少?

        解題


        解法:遞歸
        當(dāng)n==1時(shí),return 0;
        將int型的值傳給m時(shí),需要將m的類型申明為long long型。因?yàn)槿绻鹖nt 型的n為2147483647時(shí),m=(n+1)/2的過程中n+1就會(huì)溢出。
        int, long int都是4個(gè)字節(jié)的有符號(hào)數(shù),最大值為2147483647.
         
        可以將int型的實(shí)參傳遞給long long型的形參。

        class Solution {
        public:
            int integerReplacement(long long n) {
                if(n==1) return 0;
                else if(n%2==0) return 1+integerReplacement(n/2);
                else 
                {
                    long long m=n+1;
                    return 2+min(integerReplacement(m/2),integerReplacement((n-1)/2));
                }
            }
        };



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

        上期推文:

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

        LeetCode刷題實(shí)戰(zhàn)381:O(1) 時(shí)間插入、刪除和獲取隨機(jī)元素

        LeetCode刷題實(shí)戰(zhàn)382:鏈表隨機(jī)節(jié)點(diǎn)

        LeetCode刷題實(shí)戰(zhàn)383:贖金信

        LeetCode刷題實(shí)戰(zhàn)384:打亂數(shù)組

        LeetCode刷題實(shí)戰(zhàn)385:迷你語(yǔ)法分析器

        LeetCode刷題實(shí)戰(zhàn)386:字典序排數(shù)
        LeetCode刷題實(shí)戰(zhàn)387:字符串中的第一個(gè)唯一字符
        LeetCode刷題實(shí)戰(zhàn)388:文件的最長(zhǎng)絕對(duì)路徑
        LeetCode刷題實(shí)戰(zhàn)389:找不同
        LeetCode刷題實(shí)戰(zhàn)390:消除游戲
        LeetCode刷題實(shí)戰(zhàn)391:完美矩形
        LeetCode刷題實(shí)戰(zhàn)392:判斷子序列
        LeetCode刷題實(shí)戰(zhàn)393:UTF-8 編碼驗(yàn)證
        LeetCode刷題實(shí)戰(zhàn)394:字符串解碼
        LeetCode刷題實(shí)戰(zhàn)395:至少有 K 個(gè)重復(fù)字符的最長(zhǎng)子串
        LeetCode刷題實(shí)戰(zhàn)396:旋轉(zhuǎn)函數(shù)

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

        手機(jī)掃一掃分享

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

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            欧美另类高清zo欧美 | 中文字幕人乱码中文字的预防方法 | 天天日天天 | 麻豆国产电影在线 | 欧美性猛交XXX乱大交3 | 美国 日本 韩国三级三级三级黄色A在线播放 | 成人V | 色偷偷888欧美精品久久久 | 天天干天天日天天干天天日 | 亚洲日韩色图 |