JAVA solution using TreeSet


  • 0
    S
    public int nthUglyNumber(int n) {
         
            TreeSet<Long> hs = new TreeSet<>(Arrays.asList(1l, 2l, 3l, 5l));
            Long e = 1l;
            while( n != 0){
                e = hs.pollFirst();
                hs.add(e * 2);
                hs.add(e * 3);
                hs.add(e * 5);
                n--;
                
            }
            return e.intValue();
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.