<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)479:最大回文數(shù)乘積

          共 1709字,需瀏覽 4分鐘

           ·

          2021-12-28 13:45

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

          今天和大家聊的問題叫做?最大回文數(shù)乘積,我們先來看題面:
          https://leetcode-cn.com/problems/largest-palindrome-product/

          Given?an?integer?n,?return?the?largest?palindromic?integer?that?can?be?represented?as?the?product?of?two?n-digits?

          integers.?Since?the?answer?can?be?very?large,?return?it?modulo?1337.


          你需要找到由兩個 n 位數(shù)的乘積組成的最大回文數(shù)。
          由于結(jié)果會很大,你只需返回最大回文數(shù) mod 1337得到的結(jié)果。

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

          示例:

          輸入: 2
          輸出: 987
          解釋: 99 x 91 = 9009, 9009 % 1337 = 987

          說明:
          n 的取值范圍為?[1,8]。


          解題

          舉個例子:
          max = 99;
          從i= 98開始循環(huán)
          構(gòu)造出回文數(shù) rev = 9889
          對于 x = 99 ,rev不能整除,繼續(xù)
          對于 x = 98 , 98 * 98 = 9604,小于rev,退出第二層循環(huán)
          ...
          ...
          直到i= 90
          構(gòu)造出回文數(shù)9009

          class?Solution?{
          ??????public?int?largestPalindrome(int?n)?{
          ????????if(n == 1) return?9;
          ????????long?max = (long)Math.pow(10,n) - 1;
          ????????for(long?i = max - 1; i > max / 10; i--){
          ????????????String s1 = String.valueOf(i);
          ????????????long?rev = Long.parseLong(s1 + new?StringBuilder(s1).reverse().toString());
          ????????????for(long?x = max; x * x >= rev; x --){
          ????????????????if(rev % x == 0) return?(int)(rev % 1337);
          ????????????}
          ????????}
          ????????return?-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:凸多邊形

          LeetCode刷題實戰(zhàn)470:用 Rand7() 實現(xiàn) Rand10()

          LeetCode刷題實戰(zhàn)471:編碼最短長度的字符串

          LeetCode刷題實戰(zhàn)472:連接詞

          LeetCode刷題實戰(zhàn)473:火柴拼正方形

          LeetCode刷題實戰(zhàn)474:一和零

          LeetCode刷題實戰(zhàn)475:供暖器

          LeetCode刷題實戰(zhàn)476:數(shù)字的補(bǔ)數(shù)

          LeetCode刷題實戰(zhàn)477:漢明距離總和


          瀏覽 71
          點贊
          評論
          收藏
          分享

          手機(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>
                  91A视频 | 中文丰满亲子伦在线观看 | 火爆达人金先生短发白领 | 人操久久 | 欧美性爱免费在线视频PK视频 |