Nav: << previous: 263.丑数 | next: 265.粉刷房子 II >>


Description

tab: English
 
<p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
 
<p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p>
 
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
 
<pre>
<strong>Input:</strong> n = 10
<strong>Output:</strong> 12
<strong>Explanation:</strong> [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers.
</pre>
 
<p><strong class="example">Example 2:</strong></p>
 
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
</pre>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li><code>1 &lt;= n &lt;= 1690</code></li>
</ul>
 
 
 
> [!tip]- Hint 1
> 
> The naive approach is to call <code>isUgly</code> for every number until you reach the n<sup>th</sup> one. Most numbers are <i>not</i> ugly. Try to focus your effort on generating only the ugly ones.
 
> [!tip]- Hint 2
> 
> An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
 
> [!tip]- Hint 3
> 
> The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L<sub>1</sub>, L<sub>2</sub>, and L<sub>3</sub>.
 
> [!tip]- Hint 4
> 
> Assume you have U<sub>k</sub>, the k<sup>th</sup> ugly number. Then U<sub>k+1</sub> must be Min(L<sub>1</sub> * 2, L<sub>2</sub> * 3, L<sub>3</sub> * 5).
 
 
---
 
[submissions](https://leetcode.com/problems/ugly-number-ii/submissions/) | [solutions](https://leetcode.com/problems/ugly-number-ii/solutions/)
 
 
tab: 中文
 
<p>给你一个整数 <code>n</code> ,请你找出并返回第 <code>n</code> 个 <strong>丑数</strong> 。</p>
 
<p><strong>丑数 </strong>就是质因子只包含&nbsp;<code>2</code>、<code>3</code> 和&nbsp;<code>5</code>&nbsp;的正整数。</p>
 
<p>&nbsp;</p>
 
<p><strong>示例 1:</strong></p>
 
<pre>
<strong>输入:</strong>n = 10
<strong>输出:</strong>12
<strong>解释:</strong>[1, 2, 3, 4, 5, 6, 8, 9, 10, 12] 是由前 10 个丑数组成的序列。
</pre>
 
<p><strong>示例 2:</strong></p>
 
<pre>
<strong>输入:</strong>n = 1
<strong>输出:</strong>1
<strong>解释:</strong>1 通常被视为丑数。
</pre>
 
<p>&nbsp;</p>
 
<p><strong>提示:</strong></p>
 
<ul>
	<li><code>1 &lt;= n &lt;= 1690</code></li>
</ul>
 
 
 
> [!tip]- 提示 1
> 
> The naive approach is to call <code>isUgly</code> for every number until you reach the n<sup>th</sup> one. Most numbers are <i>not</i> ugly. Try to focus your effort on generating only the ugly ones.
 
> [!tip]- 提示 2
> 
> An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
 
> [!tip]- 提示 3
> 
> The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L<sub>1</sub>, L<sub>2</sub>, and L<sub>3</sub>.
 
> [!tip]- 提示 4
> 
> Assume you have U<sub>k</sub>, the k<sup>th</sup> ugly number. Then U<sub>k+1</sub> must be Min(L<sub>1</sub> * 2, L<sub>2</sub> * 3, L<sub>3</sub> * 5).
 
 
---
 
[提交记录](https://leetcode.cn/problems/ugly-number-ii/submissions/) | [题解](https://leetcode.cn/problems/ugly-number-ii/solution/)
 
 

Solutions & Notes

properties:
  note.updated:
    displayName: Last Updated
  note.relative_links:
    displayName: Related Links
  note.desc:
    displayName: Description
  note.grade:
    displayName: Rating
  note.program_language:
    displayName: Language
  note.time_complexity:
    displayName: TC
  note.space_complexity:
    displayName: SC
views:
  - type: table
    name: Solutions & Notes
    filters:
      and:
        - file.hasLink(this.file)
        - file.tags.containsAny("leetcode/solution", "leetcode/note")
    order:
      - file.name
      - desc
      - program_language
      - time_complexity
      - space_complexity
      - grade
      - relative_links
      - updated
    sort:
      - property: grade
        direction: ASC
      - property: time_complexity
        direction: ASC
      - property: program_language
        direction: ASC
    columnSize:
      file.name: 104
      note.space_complexity: 65
      note.grade: 126
 

Similar Problems

properties:
  note.lcTopics:
    displayName: Topics
  note.lcAcRate:
    displayName: AC Rate
  note.favorites:
    displayName: Favorites
  note.grade:
    displayName: Rating
  note.translatedTitle:
    displayName: Title (CN)
  note.lcDifficulty:
    displayName: Difficulty
views:
  - type: table
    name: Similar Problems
    filters:
      and:
        - file.hasLink(this.file)
        - similarQuestions.contains(this.file)
    order:
      - file.name
      - translatedTitle
      - lcTopics
      - lcDifficulty
      - lcAcRate
      - grade
      - favorites
    sort:
      - property: file.name
        direction: ASC
      - property: lcTopics
        direction: DESC
    columnSize:
      note.translatedTitle: 240
      note.lcTopics: 347
      note.lcAcRate: 75
      note.grade: 122