Ugly Number II
注意负数,所以要使用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的更多相关文章
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 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 ...
- 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] 264. Ugly Number II (medium)
263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...
- Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)
Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...
- 【刷题-LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 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 ...
- [LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- Leetcode 264. Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
随机推荐
- [LintCode] Mini Twitter 迷你推特
Implement a simple twitter. Support the following method: postTweet(user_id, tweet_text). Post a twe ...
- Color Processing 色彩处理
How to read and write pixel data Bitmap processing Image Stride Bayer Color Filter Pattern High-Qual ...
- android基础知识之一
1:Android系统架构(重点) 分层的架构 JNI java native interface 1.application :应用层 : java 2.application framework ...
- MyEclipse修改项目名称后,部署到tomcat问题
问题描述: 修改项目名称后,部署到tomcat server,部署出来的文件夹名还是旧的名称. 解决方案: 光把项目重命名是不够的,还要修改一下Myeclipse里面的配置. 工程名->右键-& ...
- BizTalk开发系列(二十六) 使用Web Service
Web Service是在构建SOA平台中广泛使用的技术.在BizTalk开发过程中使用SOAP适配器接收和发送 Web Services 请求.业务流程可以发布为 Web Services 并使用外 ...
- hdu 4897 Little Devil I
传送阵:http://acm.hdu.edu.cn/showproblem.php?pid=4897 题目大意:一棵树,三个操作:1.将某条链取反,2.将与某条链相邻的边取反,3.查询某条链上为1的边 ...
- JSP Standard Tag Library JSP标准标签库
了解了基本的标签的底层实现,可以看系统定义的强大的标准标签 1.首先引入两个jar包 2.基本语法 <%@ taglib prefix="c" uri="http: ...
- 关于mac下面用Parallels装ubuntu时分辨率问题[已解决]
最近由于要深入下Android系统,在Parallels虚拟机上安装了ubuntu系统,刚开始安装成功的时候,分辨率只有800*600.本来想着去设置里面改动呢,可是坑爹的是设置里面只有800*600 ...
- 检测PC端和移动端的方法总结(转)
正在苦逼的实习中,昨天公司让做一个页面,涉及到检测终端的问题,如果是手机设备,就跳转到指定的网页上,以前写响应式布局只要用@media screen来实现布局的差异化适应,但是现在不仅仅是布局,还要针 ...
- github展示项目
首先在原repo中创建一个gh-pages分支,然后把你master分支的东西都搬过来,将这个gh-pages作为你的HEAD主分支.如果想删掉master分支,需要在repo的settings中修改 ...