leetcode 264. Ugly Number II
Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
Example:
Input: n = 10
Output: 12
Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
Note:
1 is typically treated as an ugly number.
n does not exceed 1690.
class Solution {
public int nthUglyNumber(int n) {
int[] arr = new int;
int t2 = 1, t3 = 1, t5 = 1;
arr = 1;
for(int i = 2 ; i <= n ; i++){
arr = Math.min (Math.min(2*arr, 3*arr),5*arr);
if(arr == 2*arr) t2++;
if(arr == 3*arr) t3++;
if(arr == 5*arr) t5++;
}
return arr;
}
}
页:
[1]