注意负数,所以要使用long,而不能用int

详细解释 请参见http://www.cnblogs.com/julie-yang/p/5147460.html

#include<vector>
#include<queue>
#include<map>
#include<limits>
#include<iostream>
using namespace std;
struct Node{
long idx;
long val;
};
struct cmp
{ bool operator()(const Node &a,const Node &b)
{
return a.val>b.val;
}
};
class Solution {
public:
int nthUglyNumber(int n) {
priority_queue<Node, vector<Node>, cmp> min_heap;
vector<int> primes;
primes.push_back();
primes.push_back();
primes.push_back();
if (primes.size() == ) return ;
map<long, int> start_idx;
start_idx[] = ;
int res = ; Node temp_node;
temp_node.idx = ; //idx is the first val of start_idx
temp_node.val = primes[];
min_heap.push(temp_node); int cnt = ;
if (n == ) return ;
long min_val = INT_MAX; long min_idx = ;
while (cnt < n)
{
min_val = min_heap.top().val;
min_idx = min_heap.top().idx;
min_heap.pop();
if ((res != (min_val)))
{
res = min_val;
cnt++;
start_idx[min_val] = ;
temp_node.idx = min_val; //idx is the first val of start_idx
temp_node.val = min_val * primes[];
min_heap.push(temp_node); }
if (start_idx[min_idx] < primes.size() - )
{
start_idx[min_idx] ++;
temp_node.idx = min_idx; //idx is the first val of start_idx
temp_node.val = min_idx * primes[start_idx[min_idx]];
min_heap.push(temp_node); } }
return res;
}
};

Ugly Number II的更多相关文章

  1. 【LeetCode】264. Ugly Number II

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

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

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

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

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

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

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

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

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

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

  8. [LeetCode] Ugly Number II 丑陋数之二

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

  9. Leetcode 264. Ugly Number II

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

  10. [LeetCode] Ugly Number II

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

随机推荐

  1. android夜间模式实现

    一.概述 android夜间模式实现分为两大类 重启activity的实现 不重启activity的实现 二.正文 1.重启activity实现夜间模式[在界面文件中的实现部分] 1.1在attrs. ...

  2. zk master-slaver机制

    1.基本概念 >>zookeeper handler (zk句柄)有点类似文件句柄,打开一个文件就保持了一个文件句柄!同样的道理: 建立一个到zk server的session就会有一个z ...

  3. 32位的Win7系统下安装64位的Sql Sever?

    来自:http://zhidao.baidu.com/link?url=nQBoaLgoOyYCUdI7V4WZCMlTW3tKscdkOnLTIvlYtPpwoVhQkSahq44HeofBfzFT ...

  4. Linux_Shell

    一.Shell 种类与归属 Unix与Linux常见的Shell脚本解释器有bash,sh,csh,ksh等(PS: bash 完全兼容sh) bash : linux 默认的shell sh : u ...

  5. Ajax Post 与 Get 实例

    Ajax的POST实例,index.html <html> <head> <script type="text/javascript"> fun ...

  6. Linux 搭建svn服务器

    一.安装svn服务器端yum install subversion      从镜像下载安装svn服务器端 如果后面执行“svnadmin create /usr/local/svn/sunny”提示 ...

  7. 【iCore3 双核心板_FPGA】例程十三:FSMC总线通信实验——复用地址模式

    实验指导书及代码包下载: http://pan.baidu.com/s/1nuYpI8x iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  8. PHP检测移动设备类mobile detection使用实例

    目前,一个网站有多个版本是很正常的,如PC版,3G版,移动版等等.根据不同的浏览设备我们需要定向到不同的版本中.不仅如此,我们有时候还需要根据不同的客户端加载不同的CSS,因此我们需要能够检测浏览设备 ...

  9. Android NDK开发之Jni调用Java对象

    https://my.oschina.net/zhiweiofli/blog/114064 通过使用合适的JNI函数,你可以创建Java对象,get.set 静态(static)和 实例(instan ...

  10. 【C++】类型转换(学习笔记)

    1. 隐式类型转换,相关联的类型(e.g.int vs double)之间可以发生隐式类型转换. 比如,在条件中,非布尔类型转为布尔类型: 初始化时,初始值变为变量类型: 赋值时,右值变成左侧的类型: ...