方案一:最小堆。
方案二:三指针法。
# 编写一个程序,找出第 n 个丑数。## 丑数就是质因数只包含 2, 3, 5 的正整数。## 示例:## 输入: n = 10# 输出: 12# 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。## 说明:### 1 是丑数。# n 不超过1690。## Related Topics 堆 数学 动态规划# 👍 361 👎 0from heapq import *# leetcode submit region begin(Prohibit modification and deletion)class Solution(object):def nthUglyNumber(self, n):""":type n: int:rtype: int"""num = [1]p2 = p3 = p5 = 0while len(num) < n:ugly = min(num[p2] * 2, num[p3] * 3, num[p5] * 5)num.append(ugly)if ugly == num[p2] * 2:p2 += 1if ugly == num[p3] * 3:p3 += 1if ugly == num[p5] * 5:p5 += 1return num[n - 1]# leetcode submit region end(Prohibit modification and deletion)s = Solution()print(s.nthUglyNumber(10))
