标签:
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.
标签:
原文地址:http://www.cnblogs.com/shuashuashua/p/5643744.html