【leetcode】313. Super Ugly Number
题目如下:

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

代码如下:
#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的更多相关文章
- 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 ...
- [LeetCode] 313. Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- Leetcode 313. super ugly number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- 313. Super Ugly Number
题目: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【leetcode】668. Kth Smallest Number in Multiplication Table
题目如下: 解题思路:几乎和[leetcode]719. Find K-th Smallest Pair Distance 的方法一样.只不过一个是减法一个是乘法,还有一点区别是[leetcode]7 ...
- [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了
丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...
- 【LeetCode】887. Super Egg Drop 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 参考资料 日期 题目地址:https://leetc ...
- 【leetcode】414. Third Maximum Number
problem 414. Third Maximum Number solution 思路:用三个变量first, second, third来分别保存第一大.第二大和第三大的数,然后遍历数组. cl ...
随机推荐
- 阶段3 1.Mybatis_11.Mybatis的缓存_4 mybatis一对多实现延迟加载
改成单表查询 首先配置的是select.他需要配置的值是accountDao中的方法,查询所有的账户,但是必须有条件.根据用户的id column配置的是id.因为要用user表的id去关联查询 Ac ...
- C# DES 加解密
using System; using System.Text; using System.Security.Cryptography; using System.Diagnostics; using ...
- SpringBoot项目启动时执行初始化操作
SpringBooot中的CommandLineRunner接口会在所有Spring Beans初始化之后,SpringApplication.run()之前执行. 1.添加pom引用 <?xm ...
- 关于Maven的安装和配置
1.Maven的介绍 1.Maven是一个项目管理工具(项目对象模型POM) 2.Maven可以管理项目中的jar包依赖 3.Maven的中央仓库地址 http://mvnrepository.com ...
- Spark-Core RDD转换算子-kv型
大多数的 Spark 操作可以用在任意类型的 RDD 上, 但是有一些比较特殊的操作只能用在key-value类型的 RDD 上. 这些特殊操作大多都涉及到 shuffle 操作, 比如: 按照 ke ...
- 线性表源码分享(c++),包含顺序表、单链表、循环链表、双向链表
---恢复内容开始--- 我是一个c++和数据结构的初学者,本文主要是把清华大学出版社的数据结构(用面向对象方法与c++语言描述)(第2版)这本书中第二章线性表的源码抄下来,在学习的过程中有助于加深印 ...
- python中输入三个整数x,y,z,请把这三个数由小到大输出。
输入三个整数x,y,z,请把这三个数由小到大排序,再把数组由大到小排序,再输出最大值和最小值! #定义一个空数组 numbers = [] #循环遍历,下面的4是控制循环次数 for i in ran ...
- 在.NET使用Newtonsoft.Json转换,读取,写入json
首先,大家要明白什么是json,了解更多关于json方面资料大家可以点击https://www.ibm.com/developerworks/cn/web/wa-lo-json/ ,我在这里简单介绍下 ...
- jQuery之样式的类操作
方法:添加类addClass .删除类removeClass. 切换类toggleClass <style> div { width: 150px; height: 150px; b ...
- 2017第二届广东省强网杯线上赛--Nonstandard
测试文件:http://static2.ichunqiu.com/icq/resources/fileupload/CTF/echunqiu/qwb/Nonstandard_26195e1832795 ...