鱼C论坛

 找回密码
 立即注册
查看: 1553|回复: 0

[学习笔记] leetcode 264. Ugly Number II

[复制链接]
发表于 2019-9-24 06:21:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. Write a program to find the n-th ugly number.

  2. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.

  3. Example:

  4. Input: n = 10
  5. Output: 12
  6. Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
  7. Note:  

  8. 1 is typically treated as an ugly number.
  9. n does not exceed 1690.
复制代码

  1. class Solution {
  2.     public int nthUglyNumber(int n) {
  3.         int[] arr = new int[n+1];
  4.         int t2 = 1, t3 = 1, t5 = 1;
  5.         arr[1] = 1;
  6.         for(int i = 2 ; i <= n ; i++){
  7.             arr[i] = Math.min (Math.min(2*arr[t2], 3*arr[t3]),5*arr[t5]);
  8.             
  9.             if(arr[i] == 2*arr[t2]) t2++;
  10.             if(arr[i] == 3*arr[t3]) t3++;
  11.             if(arr[i] == 5*arr[t5]) t5++;
  12.         }
  13.         
  14.         return arr[n];
  15.         
  16.     }
  17.    
  18. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-2 00:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表