Skip to content

Instantly share code, notes, and snippets.

@leearmee35
Created March 5, 2017 04:18
Show Gist options
  • Save leearmee35/2f2dc5ea0003f34f0382fdba4faa3389 to your computer and use it in GitHub Desktop.
Save leearmee35/2f2dc5ea0003f34f0382fdba4faa3389 to your computer and use it in GitHub Desktop.
264. Ugly Number II
public class Solution {
public int nthUglyNumber(int n) {
PriorityQueue<Long> list = new PriorityQueue<Long>();
HashMap<Long, Integer> map = new HashMap<Long, Integer>();
long kkk = 1;
list.add(kkk);
long[] nums = new long[3];
nums[0] = 2;
nums[1] = 3;
nums[2] = 5;
while(list.size()>0){
long k = list.poll();
n--;
for(int i=0;i<3;i++){
if(map.get(nums[i]*k)==null){
list.offer(nums[i] * k);
map.put(nums[i]*k,1);
//System.out.println(nums[i]*k);
}
}
if(n==0) return (int)k;
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment