【LeetCode】264. Ugly Number II
Ugly Number II
Write a program to find the n-th ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
Note that 1 is typically treated as an ugly number.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
思路:所有的ugly number都是由1开始,乘以2/3/5生成的。
只要将这些生成的数排序即可获得,自动排序可以使用set
这样每次取出的第一个元素就是最小元素,由此再继续生成新的ugly number.
class Solution {
public:
int nthUglyNumber(int n) {
set<long long> order;
order.insert();
int count = ;
long long curmin = ;
while(count < n)
{
curmin = *(order.begin());
order.erase(order.begin());
order.insert(curmin * );
order.insert(curmin * );
order.insert(curmin * );
count ++;
}
return (int)curmin;
}
};

【LeetCode】264. Ugly Number II的更多相关文章
- 【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...
- 【Leetcode】264. Ugly Number II ,丑数
原题 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime facto ...
- 【刷题-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】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】263. Ugly Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 除去2,3,5因子 日期 [LeetCode] 题目 ...
- 【LeetCode】263. Ugly Number
Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are posi ...
- 【LeetCode】137. Single Number II (3 solutions)
Single Number II Given an array of integers, every element appears threetimes except for one. Find t ...
- 【leetcode】1201. Ugly Number III
题目如下: Write a program to find the n-th ugly number. Ugly numbers are positive integers which are div ...
- 【LeetCode】137. Single Number II
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
随机推荐
- 配置org.springframework.scheduling.quartz.CronTriggerBean(转)
注意:定时器方法里如果执行动作的时间超出了定时器的周期,将会产生两个方法同时执行的情况. 一个Quartz的CronTrigger表达式分为七项子表达式,其中每一项以空格隔开,从左到右分别是:秒,分, ...
- VB.NET读取Excel工作表信息
Dim dt As DataTable = ExcelConn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, Nothing)
- Java入门1dayCode
public class HelloWorld { /* * 多行注释方式 * main()方法: java语言的入口方法(函数) */ public static void main(String[ ...
- jquery_easyui 相关问题
1. datagrid点击title,无法进行客户端排序. 增加属性 data-options="singleSelect:true,collapsible:true,url:'/ViewS ...
- haskell中的cps
cps全称叫continuation passing style,简要来讲就是告诉函数下一步做什么的递归方式,由于普通递归有栈溢出的问题,而cps都是尾递归(tail recursion),尾递归则是 ...
- php数据库常用函数
//打开mysqlmysql_connect( $host, $user, $pwd ) or die('error');$host => localhost //数据库地址$user => ...
- SQL Server 存储引擎-剖析Forwarded Records
我们都知道数据在存储引擎中是以页的形式组织的,但数据页在不同的组织形式中其中对应的数据行存储是不尽相同的,这里通过实例为大家介绍下堆表的中特有的一种情形Forwared Records及处理方式. 概 ...
- Upstart 1.10 发布,系统初始化守护进程
Upstart 是一个用以替换 /sbin/init 守护进程的软件,基于事件机制开发.可用来处理启动过程中的任务和服务启动. Upstart 1.10 发布,改进记录: New bridges: u ...
- BugTracker 功能说明(有图有真相)
一.简单介绍 BugTracker是基于Asp.Net,C#, SqlServer的一个web端Bug管理系统.发布在IIS上.能够对不同项目,不同组织,不同人员的bug进行管理和更新设计优先级和用户 ...
- 将doc文件批量转为pdf文件
需要将不少doc文件转为pdf,WPS带有这种功能,但是鼠标点击次数太多以后整个人都变得很烦躁 用了一下午去搜这方面的工具软件,找到若干.有一些免费,有一些试用的,但总归就找到一个真正能用,虽说生成的 ...