思路:

新生成的数字一定是原来的某个数字乘以2、3或5,为了得到最小的一个,需要用三个指针记录原数字的位置以供比较。为了避免重复,生成新数字以后,原数字对应的指针需要后移一下。

实现:

 class Solution
{
public:
int nthUglyNumber(int n)
{
vector<int> dp(n);
dp[] = ;
int p2 = , p3 = , p5 = ;
for (int i = ; i < n; i++)
{
dp[i] = min(dp[p2] * , min(dp[p3] * , dp[p5] * ));
if (dp[i] == dp[p2] * ) p2++;
if (dp[i] == dp[p3] * ) p3++;
if (dp[i] == dp[p5] * ) p5++;
}
return dp[n - ];
}
};

leetcode264 Ugly Number II的更多相关文章

  1. Leetcode264. Ugly Number II丑数2

    编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...

  2. 【LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  3. Ugly Number,Ugly Number II,Super Ugly Number

    一.Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are po ...

  4. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  5. [leetcode] 264. Ugly Number II (medium)

    263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...

  6. Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

    Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...

  7. 【刷题-LeetCode】264. Ugly Number II

    Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...

  8. [Swift]LeetCode264.丑数 II | Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  9. leetcode@ [263/264] Ugly Numbers & Ugly Number II

    https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...

随机推荐

  1. Ubuntu清理内存命令(效果不明显)

    注意:最好不要在生产环境上使用!!! 1.检查内存使用情况 watch -n 3 free -m watch -n 3 cat /proc/meminfo 2.清理 #释放页缓存 echo 1 > ...

  2. SystemTap 学习笔记 - 安装篇

    https://segmentfault.com/a/1190000000671438 在安装前,需要知道下自己的系统环境,我的环境如下: uname -r 2.6.18-308.el5 Linux ...

  3. angularJS 系列(七)

    In AngularJS 1.3+, there is One-time binding built-in: The main purpose of one-time binding expressi ...

  4. [Debug] Node-sass

    Meet some problem when trying to install node-sass on windwos. Company has proxy settings, need to r ...

  5. 魔兽争霸3 冰封王座 w3g文件如何打开

    w3g文件怎么样才能看??? 满意回答 检举|2011-11-10 11:23 你应该是玩魔兽争霸的吧,如果是就找到你魔兽安装文件夹里面有个replay的文件夹,把w3g格式的文件放入该文件夹,再进入 ...

  6. paramiko_su_root

    #coding=utf8 import paramiko import time import logging ''' if user root,can not login,must use user ...

  7. chrome.socket

    chrome.socket https://chajian.baidu.com/developer/apps/socket.html#method-create 描述: 使用 chrome.socke ...

  8. Linux 编译C++ 与 设置 Vim

    1. Linux 下编译c++ vim test.cpp    // 创建文件 g++ test.cpp    // 编译文件 ./a.out         // 执行文件 g++ test.cpp ...

  9. 读书笔记:Information Architecture for the World Wide Web, 3rd Edition 北极熊 简介

    书籍介绍 Information Architecture for the World Wide Web, 3rd Edition<web信息架构:设计大型网站(第三版)> Designi ...

  10. 进程间通信之-共享内存Shared Memory--linux内核剖析(十一)

    共享内存 共享内存是进程间通信中最简单的方式之中的一个. 共享内存是系统出于多个进程之间通讯的考虑,而预留的的一块内存区. 共享内存同意两个或很多其他进程訪问同一块内存,就如同 malloc() 函数 ...