注意负数,所以要使用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. [LintCode] Pow(x, n) 求x的n次方

    Implement pow(x, n). Notice You don't need to care about the precision of your answer, it's acceptab ...

  2. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

  3. winform制作简单计算器

    public Form1() { InitializeComponent(); textBox2.Text = ";//主显示屏 textBox1.Text = "";/ ...

  4. AsyncTask的使用

    简单的AnsyTask的使用demo 1.定义一个模拟网络操作的类 package com.example.administrator.myapplication; /** * Created by ...

  5. 论meta name= viewport content= width=device-width initial-scale=1 minimum-scale=1 maximum-scale=1的作用

    一.先明白几个概念 phys.width: device-width: 一般我们所指的宽度width即为phys.width,而device-width又称为css-width. 其中我们可以获取ph ...

  6. jquery_事件与动画

    事件绑定 bind(type[,data],fn)(无限触发) type:事件类型包括jquery中已有事件也可以自定义事件 data:可选参数,作为event.data属性传递给事件对象的额外数据对 ...

  7. BizTalk动手实验(六)Orchestration开发

    1 课程简介 通过本课程熟悉Orchestration的相关开发与测试技术 2 准备工作 熟悉XML.XML Schema.XSLT等相关XML开发技术 熟悉.NET相关开发技术 新建BizTalk空 ...

  8. BZOJ4443:[SCO2015]小凸玩矩阵

    题目大意:给一个N*M的矩阵,选出N个数,使得每行没列只选一个数,求第K大的数的最小值是多少? 二分答案,第k大的数<=x,则有N-k+1个数<=k,用二分图判定. #include< ...

  9. mysql5.5手册读书日记(3)

    <?php /* MySQL_5.5中文参考手册 587开始 与GROUP BY子句同时使用的函数和修改程序 12.10.1. GROUP BY(聚合)函数 12.10.2. GROUP BY修 ...

  10. SharePoint 2013 网站应用程序、网站集、网站知识整理

    网站应用程序:Web 应用程序是一种可以通过Web访问的应用程序.我们自己以前用VS开发的Web应用程序一般是通过人工部署到IIS上的,而SharePoint的Web应用程序是由SharePoint安 ...