Ugly Number II
2016-10-24 07:36
99 查看
Write a program to find the
Ugly numbers are positive numbers whose prime factors only include
Note that
3 pointer index2, index3, index5
n-th ugly number.
Ugly numbers are positive numbers whose prime factors only include
2, 3, 5. For example,
1, 2, 3, 4, 5, 6, 8, 9, 10, 12is the sequence of the first
10ugly numbers.
Note that
1is typically treated as an ugly number.
3 pointer index2, index3, index5
public class Solution { public int nthUglyNumber(int n) { int isUglyNum[] = new int ; isUglyNum[0] = 1; int count = 1; int index2 = 0; int index3 = 0; int index5 = 0; while(count < n){ isUglyNum[count] = Math.min(isUglyNum[index2] * 2, Math.min(isUglyNum[index3] * 3, isUglyNum[index5] * 5)); if(isUglyNum[count] == isUglyNum[index2] * 2) index2 ++; if(isUglyNum[count] == isUglyNum[index3] * 3) index3 ++; if(isUglyNum[count] == isUglyNum[index5] * 5) index5 ++; count++; } return isUglyNum[n-1]; } }
相关文章推荐
- LeetCode#264 Ugly Number II (week8)
- leetcode Ugly Number II
- leetCode 264. Ugly Number II
- leetcode:数学: Ugly Number II(264)
- 264. Ugly Number II&丑数
- [C++]Ugly Number II丑数2
- LeetCode---ugly number I and II
- 算法分析与设计课程(19):【leetcode】Ugly Number II
- leetcode 263&264: Ugly Number I & II
- leetcode-Ugly Number II
- 264、Ugly Number II
- Middle-题目81:264. Ugly Number II
- Leetcode 264. Ugly Number II
- 264. Ugly Number II
- leetcode 264. Ugly Number II
- LintCode 4:Ugly Number II
- 264. Ugly Number II
- <LeetCode OJ> Ugly Number / Ugly Number II【263 / 264】
- Ugly Number II
- LeetCode 264 Ugly Number II (递推)