O(n) Java solution
The idea of this solution is from this page:http://www.geeksforgeeks.org/ugly-numbers/\n   \nThe ugly-number sequence is 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, \u2
The idea of this solution is from this page:http://www.geeksforgeeks.org/ugly-numbers/\n   \nThe ugly-number sequence is 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, \u2026\nbecause every number can only be divided by 2, 3, 5, one way to look at the sequence is to split the sequence to three groups as below:\n\n    (1) 1\xd72, 2\xd72, 3\xd72, 4\xd72, 5\xd72, \u2026\n    (2) 1\xd73, 2\xd73, 3\xd73, 4\xd73, 5\xd73, \u2026\n    (3) 1\xd75, 2\xd75, 3\xd75, 4\xd75, 5\xd75, \u2026\n   \nWe can find that every subsequence is the ugly-sequence itself (1, 2, 3, 4, 5, \u2026) multiply 2, 3, 5. \n\nThen we use similar merge method as merge sort, to get every ugly number from the three subsequence.    \n\nEvery step we choose the smallest one, and move one step after,including nums with same value.\n\nThanks for this author about this brilliant idea. Here is my java solution\n\n    public class Solution {\n        public int nthUglyNumber(int n) {\n            int[] ugly = new int[n];\n            ugly[0] = 1;\n            int index2 = 0, index3 = 0, index5 = 0;\n            int factor2 = 2, factor3 = 3, factor5 = 5;\n            for(int i=1;i<n;i++){\n                int min = Math.min(Math.min(factor2,factor3),factor5);\n                ugly[i] = min;\n                if(factor2 == min)\n                    factor2 = 2*ugly[++index2];\n                if(factor3 == min)\n                    factor3 = 3*ugly[++index3];\n                if(factor5 == min)\n                    factor5 = 5*ugly[++index5];\n            }\n            return ugly[n-1];\n        }\n    }
['Java']