LeetCode264. Ugly Number II

文章目录

一、题目

An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.

Given an integer n, return the nth ugly number.

Example 1:

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.
Example 2:

Input: n = 1
Output: 1
Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.

Constraints:

1 <= n <= 1690

二、题解

class Solution {
   
public:
    int nthUglyNumber(int n) {
   
        vector<int> res(n+1,0);
        res[1] = 1;
        for(int i2 = 1,i3 = 1,i5 = 1,i = 2;i <= n;i++){
   
            int a = res[i2] * 2,b = res[i3] * 3,c = res[i5] * 5;
            int minValue = min(a,min(b,c));
            if(minValue == a) i2++;
            if(minValue == b) i3++;
            if(minValue == c) i5++;
            res[i] = minValue;
        }
        return res[n];
    }
};

相关推荐

  1. Leetcode274

    2023-12-16 05:24:07       31 阅读
  2. LeetCode264. Ugly Number II

    2023-12-16 05:24:07       52 阅读
  3. LeetCode 264 丑数II

    2023-12-16 05:24:07       35 阅读
  4. LeetCode--26

    2023-12-16 05:24:07       54 阅读
  5. leetcode 24

    2023-12-16 05:24:07       47 阅读
  6. LeetCode 224:基本计算器

    2023-12-16 05:24:07       59 阅读
  7. LeetCode204. Count Primes

    2023-12-16 05:24:07       47 阅读
  8. [leetcode 274][H指数]

    2023-12-16 05:24:07       43 阅读
  9. leetcode - 284. Peeking Iterator

    2023-12-16 05:24:07       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-16 05:24:07       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-16 05:24:07       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-16 05:24:07       87 阅读
  4. Python语言-面向对象

    2023-12-16 05:24:07       96 阅读

热门阅读

  1. 云原生之深入解析云原生架构的日志监控

    2023-12-16 05:24:07       54 阅读
  2. 【前端设计模式】之抽象工厂模式

    2023-12-16 05:24:07       54 阅读
  3. Uniapp小程序通过camera组件实现视频拍摄

    2023-12-16 05:24:07       57 阅读
  4. Ansible

    Ansible

    2023-12-16 05:24:07      43 阅读