<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)441:排列硬幣

          共 1015字,需瀏覽 3分鐘

           ·

          2021-11-19 05:35

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

          今天和大家聊的問題叫做?排列硬幣,我們先來看題面:
          https://leetcode-cn.com/problems/arranging-coins/

          You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete.


          Given the integer n, return the number of complete rows of the staircase you will build.


          你總共有 n 枚硬幣,并計(jì)劃將它們按階梯狀排列。對(duì)于一個(gè)由 k 行組成的階梯,其第 i 行必須正好有 i 枚硬幣。階梯的最后一行 可能 是不完整的。
          給你一個(gè)數(shù)字 n ,計(jì)算并返回可形成 完整階梯行 的總行數(shù)。

          示例

          示例 1:


          輸入:n = 5
          輸出:2
          解釋:因?yàn)榈谌胁煌暾苑祷?2 。

          示例 2:


          輸入:n = 8
          輸出:3
          解釋:因?yàn)榈谒男胁煌暾苑祷?3 。

          ?

          解題

          用到等差數(shù)列求和公式。用二分法來做,就是尋找最后一個(gè)等差數(shù)列之和小于等于n 的數(shù),也就是找右邊界。

          class?Solution?{
          public:
          ????int?arrangeCoins(int?n)?{
          ????????int?l = 1, r = n;
          ????????if?(n == 0)
          ????????????return?0;
          ????????while?(l < r)
          ????????{
          ????????????long?long?mid = l + (r - l + 1) / 2;
          ????????????if?((1?+ mid) * mid / 2?<= n)
          ????????????????l = mid;
          ????????????else
          ????????????????r = mid - 1;
          ????????}
          ????????return?l ;

          ????}
          };


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

          上期推文:

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



          瀏覽 45
          點(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>
                  97精品人妻 | 美女操逼黄片 | 亚洲爱爱电影 | 97中文字幕第二十二页 | 骚逼天堂|