Super Ugly Number
Posted keepshuatishuati
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Super Ugly Number相关的知识,希望对你有一定的参考价值。
1 public class Solution { 2 public int nthSuperUglyNumber(int n, int[] primes) { 3 if (n == 0 || primes.length == 0) { 4 return 1; 5 } 6 List<Integer> result = new ArrayList<>(); 7 result.add(1); 8 int[] record = new int[primes.length]; 9 Set<Integer> set = new HashSet<>(); 10 int current = 0; 11 int index = 0; 12 while (result.size() < n) { 13 current = primes[0] * result.get(record[0]); 14 index = 0; 15 for (int i = 1; i < primes.length; i++) { 16 if (current > primes[i] * result.get(record[i])) { 17 current = primes[i] * result.get(record[i]); 18 index = i; 19 } 20 } 21 if (!set.contains(current)) { 22 result.add(current); 23 set.add(current); 24 } 25 record[index]++; 26 } 27 return result.get(result.size() - 1); 28 } 29 }
1. data initialization : index = 0.
2. use a set to deduplicate the numbers.
以上是关于Super Ugly Number的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode(313):Super Ugly Number