堆 from heapq import heappop, heappushclass Solution:def nthUglyNumber(self, n):q = [1]vis = {1}for _ in range(n - 1):now = heappop(q)for i in [2, 3, 5]:if now * i not in vis:vis.add(now * i)heappush(
题目内容 https://www.nowcoder.com/practice/6aa9e04fc3794f68acf8778237ba065b?tpId=13&tqId=11186&tPage=2&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking 把只包含质因子2、3和5的数称作丑数(Ugly Nu
264. 丑数 II 给你一个整数 n ,请你找出并返回第 n 个 丑数 。 丑数 就是质因子只包含 2、3 和 5 的正整数。 class Solution {public int nthUglyNumber(int n) {int[] dp = new int[n];dp[0] = 1;int a = 0;int b = 0;int c = 0;for(int i = 1;i < n
分析: 判断一个数 n 是否是丑数,分成三个部分 1、寻找因数,从2遍历到 n,如果该数 i 是 n 的因数,就进入下一步2、判断 i 是否是质数,这部分代码直接套用即可,见得较多3、最后判断 i 是否等于2或3或5,如果等于,n 即为丑数 import java.io.*;public class Main {public static void main(String[]