博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ugly Number II
阅读量:4708 次
发布时间:2019-06-10

本文共 1415 字,大约阅读时间需要 4 分钟。

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

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

#include
#include
#include
#include
#include
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
, cmp> min_heap; vector
primes; primes.push_back(2); primes.push_back(3); primes.push_back(5); if (primes.size() == 0) return 0; map
start_idx; start_idx[1] = 0; int res = 1; Node temp_node; temp_node.idx = 1; //idx is the first val of start_idx temp_node.val = primes[0]; min_heap.push(temp_node); int cnt = 1; if (n == 1) return 1; long min_val = INT_MAX; long min_idx = 0; 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] = 0; temp_node.idx = min_val; //idx is the first val of start_idx temp_node.val = min_val * primes[0]; min_heap.push(temp_node); } if (start_idx[min_idx] < primes.size() - 1) { 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; }};

 

转载于:https://www.cnblogs.com/julie-yang/p/5147485.html

你可能感兴趣的文章
Nginx的HTTPS 301重定向到另一个TLD(托管在同一服务器上)没有显示出SSL警告
查看>>
RUBY 模拟rtsp消息
查看>>
spring与axis2整合发布webservice
查看>>
A - Financial Management(1.1.1)
查看>>
先注册,再登录(简易版)
查看>>
Java多线程通讯---------wait,notify区别
查看>>
spring.xml配置
查看>>
P1855 榨取kkksc03
查看>>
.net转java 学习笔记 (一) java环境安装和配置
查看>>
浅谈window.attachEvent
查看>>
聚类算法——KMEANS算法
查看>>
【Java】 Thinking in Java 2-11 练习10
查看>>
26. Remove Duplicates from Sorted Array
查看>>
JavaScript要点(十二) HTML DOM 事件
查看>>
记一次file_get_contents报failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request的错...
查看>>
记录一下自己的.tmux.conf,.vimrc
查看>>
Linux性能指标解释+Oracle性能指标解释
查看>>
Windows 7中怎样找到真正的Administrator账户Windows 7中怎样找到真正的Administrator账户...
查看>>
22 递归问题
查看>>
结构体中的 宏定义
查看>>