LeetCode 313 Super Ugly Number
2016-04-20 18:06
363 查看
Write a program to find the nth super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list
size
Note:
(1)
(2) The given numbers in
(3) 0 <
106, 0 <
1000.
解法一:具体思路和LeetCode 264 Ugly Number II的解法二一样
根据leetcode的提示,这个题目是可以使用heap的,可是对heap的操作又不熟悉,所以,这个算法暂时作罢。以后如果有更简洁的算法,再补上。
Math Heap
Hide Similar Problems
(M) Ugly Number II
Super ugly numbers are positive numbers whose all prime factors are in the given prime list
primesof
size
k. For example,
[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32]is the sequence of the first 12 super ugly numbers given
primes=
[2, 7, 13, 19]of size 4.
Note:
(1)
1is a super ugly number for any given
primes.
(2) The given numbers in
primesare in ascending order.
(3) 0 <
k≤ 100, 0 <
n≤
106, 0 <
primes[i]<
1000.
解法一:具体思路和LeetCode 264 Ugly Number II的解法二一样
public int nthSuperUglyNumber(int n, int[] primes) { int[] nums = new int ; nums[0] = 1; int[] index = new int[primes.length]; int cout = 1; while (cout < n) { int minNum = Integer.MAX_VALUE; for (int i = 0; i < primes.length; i++) { minNum = Math.min(nums[index[i]] * primes[i], minNum); } for (int i = 0; i < primes.length; i++) { if (nums[index[i]] * primes[i] == minNum) index[i]++; } nums[cout++] = minNum; } return nums[n - 1]; }
根据leetcode的提示,这个题目是可以使用heap的,可是对heap的操作又不熟悉,所以,这个算法暂时作罢。以后如果有更简洁的算法,再补上。
Math Heap
Hide Similar Problems
(M) Ugly Number II
相关文章推荐
- Android Selector 与 Shape 基本用法
- HCatalog
- iOS-金额小写转大写
- Eclipse开发web项目中关于HttpServletRequest包的问题
- 害人的VS2008,manifest导致“应用程序配置不正确,应用程序未能启动” [转载]
- asp SqlDataSource模糊查询的问题
- CentOS6.5 64位环境下安装Mysql
- 离散小波变换(DWT)整理
- 如何利用Node.js 构建分布式集群
- 国内有哪些好的刷题网站?
- 自定义Hive文件和记录格式
- Hive函数
- vijos P1352 最大获利(最小割)
- iOS Xcode6添加pch文件
- 在ECshop首页模板修改增加收藏本站js代码的方法
- java7中G1垃圾收集器使用和流程总结
- 二叉树的创建及成员函数的实现
- 教你认清MVC,MVP和MVVM
- IOS中的字典转模型2
- oracle oem无法打开 缘由;consoleorcl服务未打开