|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- 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[n+1];
- int t2 = 1, t3 = 1, t5 = 1;
- arr[1] = 1;
- for(int i = 2 ; i <= n ; i++){
- arr[i] = Math.min (Math.min(2*arr[t2], 3*arr[t3]),5*arr[t5]);
-
- if(arr[i] == 2*arr[t2]) t2++;
- if(arr[i] == 3*arr[t3]) t3++;
- if(arr[i] == 5*arr[t5]) t5++;
- }
-
- return arr[n];
-
- }
-
- }
复制代码 |
|