我的LeetCode代码仓:https://github.com/617076674/LeetCode
原题链接:https://leetcode-cn.com/problems/ugly-number-ii/
题目描述:
知识点:动态规划
思路一:暴力破解法(在LeetCode中提交会超时)
从0开始依次判断每一个数是否是丑数,每次递增1,直到找到第n个丑数。
时间复杂度是O(n ^ 2)。空间复杂度是O(1)。
JAVA代码:
public class Solution {
public int nthUglyNumber(int n) {
int index = 0, i = 0;
while (index < n) {
if (isUgly(i++)) {
index++;
}
}
return i - 1;
}
private boolean isUgly(int num) {
if (0 >= num) {