<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)263:丑數(shù)

          共 1414字,需瀏覽 3分鐘

           ·

          2021-05-15 09:38

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

          今天和大家聊的問(wèn)題叫做 丑數(shù),我們先來(lái)看題面:
          https://leetcode-cn.com/problems/ugly-number/

          An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.

          Given an integer n, return true if n is an ugly number.

          給你一個(gè)整數(shù) n ,請(qǐng)你判斷 n 是否為 丑數(shù) 。如果是,返回 true ;否則,返回 false 。丑數(shù) 就是只包含質(zhì)因數(shù) 2、3 和/或 5 的正整數(shù)。

          示例


          示例 1

          輸入:n = 6
          輸出:true
          解釋?zhuān)?span style="color: rgb(0, 128, 128);background: rgba(0, 0, 0, 0);display: inline;width: 6px;text-decoration: none solid rgb(0, 128, 128);font-weight: 400;font-style: normal;">6 = 2 × 3

          示例 2

          輸入:n = 8
          輸出:true
          解釋?zhuān)?span style="color: rgb(0, 128, 128);background: rgba(0, 0, 0, 0);display: inline;width: 6px;text-decoration: none solid rgb(0, 128, 128);font-weight: 400;font-style: normal;">8 = 2 × 2 × 2

          示例 3

          輸入:n = 14
          輸出:false
          解釋?zhuān)?span style="color: rgb(0, 128, 128);background: rgba(0, 0, 0, 0);display: inline;width: 13px;text-decoration: none solid rgb(0, 128, 128);font-weight: 400;font-style: normal;">14 不是丑數(shù),因?yàn)樗肆硗庖粋€(gè)質(zhì)因數(shù) 7 。

          示例 4

          輸入:n = 1
          輸出:true
          解釋?zhuān)?span style="color: rgb(0, 128, 128);background: rgba(0, 0, 0, 0);display: inline;width: 6px;text-decoration: none solid rgb(0, 128, 128);font-weight: 400;font-style: normal;">1 通常被視為丑數(shù)。


          解題


          若當(dāng)前數(shù)能整除2,3,5,則一直整除2,3,5,直到不能整除為止,判斷最后的數(shù)是否等于1即可,如果不是1就說(shuō)明還有其他因子

          class Solution {
              public boolean isUgly(int num) {
                  if(num <= 0) return false;
                  while(num % 2 == 0) num /= 2;
                  while(num % 3 == 0) num /= 3;
                  while(num % 5 == 0) num /= 5;
                  return num == 1;
              }
          }


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

          上期推文:

          LeetCode1-260題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)261:以圖判樹(shù)
          LeetCode刷題實(shí)戰(zhàn)262:行程和用戶(hù)


          瀏覽 53
          點(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>
                  久色网站| 北条麻妃中文字幕黄色片子 | 日本大香蕉在线视频 | 嫩草视频91麻豆黑人 | 国产日韩欧美在线观看 |