Share my 4ms cpp solution beats 98.9% submissions
\n\n    class Solution {\n    public:\n        int nthUglyNumber(int n) {\n            static vector ugly{0, 1};\n            static int m1 = 1, m2 = 1, m3 = 1;
 \n\n    class Solution {\n    public:\n        int nthUglyNumber(int n) {\n            static vector<int> ugly{0, 1};\n            static int m1 = 1, m2 = 1, m3 = 1;\n            if (n < ugly.size()) {\n                return ugly[n]; \n            }\n            int next = 0;\n            while(n >= ugly.size()) {\n                next = min(ugly[m1] * 2, min(ugly[m2] * 3, ugly[m3] * 5));\n                ugly.push_back(next);\n                if (ugly[m1] * 2 == next) {\n                    ++m1;\n                }\n                if (ugly[m2] * 3 == next) {\n                    ++m2;\n                }\n                if (ugly[m3] * 5 == next) {\n                    ++m3;\n                }\n            }\n            return ugly[n];\n        }\n    };
['C++']