Java Solution 11ms.


  • 0
    C
    public int nthUglyNumber(int n) {
        if (n<=0) return 0;
        if(n==1) return 1;
        
        int[] store = new int[n];
        store[0] = 1;
        int i2=0;
        int i3=0;
        int i5=0;
        for(int i =1;i<n;i++){
            store[i] = Math.min(Math.min(store[i2]*2,store[i3]*3),store[i5]*5);
            if(store[i] == store[i2]*2) i2++;
            if(store[i] == store[i3]*3) i3++;
            if(store[i] == store[i5]*5) i5++;
        }
        return store[n-1];
        
    }

Log in to reply
 

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