<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刷題實戰(zhàn)470:用 Rand7() 實現(xiàn) Rand10()

          共 1602字,需瀏覽 4分鐘

           ·

          2021-12-18 11:18

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

          今天和大家聊的問題叫做?用 Rand7() 實現(xiàn) Rand10(),我們先來看題面:
          https://leetcode-cn.com/problems/implement-rand10-using-rand7/

          Given the API rand7() that generates a uniform random integer in the range [1, 7], write a function rand10() that generates a uniform random integer in the range [1, 10]. You can only call the API rand7(), and you shouldn't call any other API. Please do not use a language's built-in random API.


          Each test case will have one internal argument n, the number of times that your implemented function rand10() will be called while testing. Note that this is not an argument passed to rand10().


          已有方法 rand7 可生成 1 到 7 范圍內(nèi)的均勻隨機(jī)整數(shù),試寫一個方法 rand10 生成 1 到 10 范圍內(nèi)的均勻隨機(jī)整數(shù)。

          不要使用系統(tǒng)的 Math.random() 方法。

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

          示例 1:
          輸入: 1
          輸出: [7]


          示例 2:
          輸入: 2
          輸出: [8,4]


          示例 3:
          輸入: 3
          輸出: [8,1,10]


          解題


          (rand_Y - 1) * X + rand_X => 可以生成[1, X*Y]的等概率隨機(jī)數(shù)。
          在本題中,可生成1-49的隨機(jī)數(shù),我們可以只取前40個,當(dāng)數(shù)字大于40時,繼續(xù)產(chǎn)生1-49的隨機(jī)數(shù),直到小于等于40停止。
          在得到1-40的隨機(jī)數(shù)后,對10取余再加1,即可得到1到10范圍內(nèi)的隨機(jī)數(shù)字

          class?Solution?{
          public:
          ????int?rand10()?{
          ????????int?ans = 0;
          ????????do{
          ????????????ans = (rand7() - 1) * 7?+ rand7();
          ????????}while(ans > 40);

          ????????return?ans % 10?+ 1;
          ????}
          };


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

          上期推文:

          LeetCode1-460題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)461:漢明距離

          LeetCode刷題實戰(zhàn)462:最少移動次數(shù)使數(shù)組元素相等 II

          LeetCode刷題實戰(zhàn)463:島嶼的周長

          LeetCode刷題實戰(zhàn)464:我能贏嗎

          LeetCode刷題實戰(zhàn)465:最優(yōu)賬單平衡

          LeetCode刷題實戰(zhàn)466:統(tǒng)計重復(fù)個數(shù)

          LeetCode刷題實戰(zhàn)467:環(huán)繞字符串中唯一的子字符串

          LeetCode刷題實戰(zhàn)468:驗證IP地址

          LeetCode刷題實戰(zhàn)469:凸多邊形


          瀏覽 33
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <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>
                  亚洲综合娱乐久久 | 天天看片天天操 | 亚洲天堂网在线视频观看 | 国产无遮挡免费观看 | 天天干天天干素人 |