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] Sort Integers II 整数排序之二
Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...
- JSON与DataTable(DataSet)相互转化
public static string CreateJsonParameters(DataTable dt) { /* /******************** ...
- MySql的join(连接)查询 (三表 left join 写法)
1.内连接:将两个表中存在连结关系的字段符合连接条件的记录形成记录集 Select A.name,B.name from A inner join B on A.id=B.id和 Select A.n ...
- array_reduce方法用回调函数迭代地将对数组的值进行操作
在处理php数组的时候,有一种需求特别的频繁,如下二维数组: $arr = array( 1=>array( 'id' => 5, 'name' => '张三' ), 2=>a ...
- Win8.1屏幕亮度自动调节关闭方法
细心的朋友会发现,Win8.1系统的笔记本屏幕亮度有时候,会根据外界光线亮度以及温度自动调节屏幕亮度,尽管看似比较智能,但有时候我们并不希望笔记本屏幕亮度受光线影响,忽暗忽亮.如果我们希望Win8笔记 ...
- angularJs表单校验(超级详细!!!)
html代码 <!DOCTYPE html> <html ng-app="angularFormCheckModule"> <head> < ...
- AJAX原理及XMLHttpRequest对象分析
今天的主题是前端都了解的AJAX,但其中都有哪些知识点,还需要深入分析. 首先揭示AJAX的字面意思,Asynchronous Javascript And XML,通俗点就是“异步Javascrip ...
- Shell displays color output
格式: echo "/033[字背景颜色;字体颜色m字符串/033[控制码" 如果单纯显示字体颜色可以固定控制码位0m. 格式: echo "/033[字背景颜色;字体颜 ...
- 【翻译】How To Tango With Django 1.5.4 第二章
2.开始吧! 准备好两个关键的安装包 Python version 2.7.5 Django version 1.5.4 2.1熟悉你自己的系统(我的是windows) 略 2.2安装软件 2.2.1 ...
- Count Complete Tree Nodes || LeetCode1
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...