【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 ...
 
随机推荐
- 如何查看MySQL执行计划
			
在介绍怎么查看MySQL执行计划前,我们先来看个后面会提到的名词解释: 覆盖索引: MySQL可以利用索引返回select列表中的字段,而不必根据索引再次读取数据文件 包含所有满足查询需要的数据的索引 ...
 - Android Studio 中配置强大的版本管理系统
			
1. 安装Git/CVS 第一步首先你需要安装Git/CVS等版本管理工具,这个请自行百度 2. 新建一个本地空仓库 新建一个仓库叫GitTest 仓库现在是空的什么都没有 找到路径,然后复制路径(这 ...
 - 第二章——建立一个HelloWorld项目,练习使用git的add/commit/push/pull/fetch/clone等基本命令。比较项目的新旧版本的差别-----答题者:徐潇瑞
			
1.首先下载安装git,很简单所以就不详细说了,当弹出一个类似的命令窗口的东西,就说明Git安装成功 2.因为Git是分布式版本控制系统,所以需要填写用户名和邮箱作为一个标识 3.接着,注册githu ...
 - Robot Framework-DatabaseLibrary数据库(MySql)
			
Robot Framework-Mac版本安装 Robot Framework-Windows版本安装 Robot Framework-工具简介及入门使用 Robot Framework-Databa ...
 - 【转载】-- vi/vim使用
			
vi/vim 基本使用方法本文介绍了vi (vim)的基本使用方法,但对于普通用户来说基本上够了!i/vim的区别简单点来说,它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所 ...
 - 客户端缓存 HTML + 远程数据 JS 的思路。
			
移动客户端,采用客户端集成 WebBrowser 的方式 ,加载远程网页的优化方案. 1. 远程 HTML版本 v1.2 一次性加载到客户端 2. 手机端打开时,检测HTML版本. 如果有新版,先更新 ...
 - C++11 并发指南后续更新
			
C++11 并发指南的第一篇是 2013 年 8 月 3 号写的,到今天(2013 年 8 月 31 号)差不多一个月了,前前后后共写了 6 章(目前共 8 篇)博客介绍 C++11 的并发编程,但还 ...
 - 使用Js脚本 修改控制IE的注册表相关设置(activex等)
			
使用Js脚本 修改控制IE的注册表相关设置(activex等) 分类: PHP2012-12-05 18:51 2035人阅读 评论(2) 收藏 举报 脚本写法: <SCRIPT LANGUAG ...
 - MVP
			
引自: http://www.cnblogs.com/Leo_wl/archive/2013/05/03/3056299.html http://www.codeproject.com/Article ...
 - MySQL数据库主键设计原则
			
目录 1. 主键定义... 5 2. 主键设计原则... 5 2.1 确保主键的无意义性... 5 2.2 采用整型主键... 5 2.3 减少主键的变动... 5 2.4 避免重复使用主键... 6 ...