题目如下:

解题思路:总结一下这么几点,一出一进,优先级队列排序,保证每次输出的都是当前的最小值。解法大致如图:

代码如下:

#include<map>
#include<queue>
using namespace std;
struct node
{
node(int k,int v)
{
key = k;
val = v;
}
friend bool operator< (node n1, node n2)
{
return n1.val > n2.val;
}
int key;
int val;
};
class Solution {
public:
int nthSuperUglyNumber(int n, vector<int>& primes) {
if (n == )
return ;
map<int,queue<int>> dic;
priority_queue<node> qi; for(size_t i=;i<primes.size();i++)
{
queue<int> v;
dic[primes[i]] = v;
qi.push(node(primes[i],primes[i]));
}
int count = ;
int res = ;
int lastV = ; while (true)
{
node v = qi.top();
qi.pop(); if (lastV == v.val)
continue;
//cout << v.val <<endl;
lastV = v.val;
for(size_t i=;i<primes.size();i++)
{
if (v.key <= primes[i])
dic[primes[i]].push((v.val * primes[i]));
} int nod = dic[v.key].front();
dic[v.key].pop();
qi.push(node(v.key,nod));
count += ;
if (count == n - )
{
res = v.val;
break;
}
}
return res;
}
};

【leetcode】313. Super Ugly Number的更多相关文章

  1. 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 ...

  2. [LeetCode] 313. Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  3. Leetcode 313. super ugly number

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  4. 313. Super Ugly Number

    题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose ...

  5. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  6. 【leetcode】668. Kth Smallest Number in Multiplication Table

    题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...

  7. [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了

    丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...

  8. 【LeetCode】887. Super Egg Drop 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 参考资料 日期 题目地址:https://leetc ...

  9. 【leetcode】414. Third Maximum Number

    problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...

随机推荐

  1. 中国MOOC_零基础学Java语言_第5周 数组_1多项式加法

    第5周编程题 查看帮助 返回   第5周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截 ...

  2. eslint+prettier 的 VSCode配置项

    { "files.autoSave": "off", "editor.fontSize": 12, "terminal.integ ...

  3. awk结合数组统计

    1.统计用户登录类型 #!/bin/bashdeclare -A  shells (定义关联数组shells)while read ll   (读取/etc/passwd,ll为变量) dotype= ...

  4. Mac搭建github Page的Hexo免费个人博客

    1.基础准备 github账号 安装git 安装node.js.npm 2.创建repo 3.配置SSH key 这一步并不重要,配置SSH key与否,并不影响博客的搭建和使用,只是配置了之后,更新 ...

  5. IP地址转换函数——inet_pton inet_ntop inet_aton inet_addr inet_ntoa

    inet_pton NAME     inet_pton - 将 IPv4 和 IPv6 地址从点分十进制转换为二进制 SYNOPSIS #include <arpa/inet.h> in ...

  6. glide使用总结

    1 glide是什么 glide是一个图片加载和缓存库. 2 glide的使用 第一,添加依赖 implementation 'com.github.bumptech.glide:glide:4.5. ...

  7. pyhton之解析html的表格

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'jiangwenwen' from bs4 import BeautifulS ...

  8. java常用类之BigDecimal

    BigDecimal 小数计算丢失精度问题 在计算机中,所有文件都是以二进制存储的,数字运算也是使用二进制进行计算的,因为计算机中不存在小数点,所以我们通常说的浮点数如float.double都是计算 ...

  9. linux的进程间通信概述

    一 进程间通信 1.1. linux内核提供多种进程间通信机制 a. 无名管道和有名管道 b. SystemV IPC:信号量.消息队列.共享内存 c. Socket域套接字 d. 信号 1.2. 无 ...

  10. React事件绑定的几种方式对比

    React事件绑定 由于类的方法默认不会绑定this,因此在调用的时候如果忘记绑定,this的值将会是undefined.通常如果不是直接调用,应该为方法绑定this.绑定方式有以下几种: 1. 在构 ...