// face questions 49: Number ugly // Title: We only comprise factors 2, 3 and 5 is referred to as the number of ugly number (Ugly Number). Seeking ascending // of 1500 the number of ugly big order. E.g. 6,8 are several ugly, but not 14, because it contains factor 7. // practice, we put 1 as the first ugly number. #include <cstdio> // Code Algorithm 1 =================== ==================== = // brute force method, time complexity is higher BOOL IsUgly ( int Number) { the while (Number% 2 == 0 ) Number / = 2 ; the while (Number% . 3 == 0 ) Number / = . 3 ; the while (number % 5 == 0) number /= 5; return (number == 1) ? true : false; } int GetUglyNumber_Solution1(int index) { if (index <= 0) return 0; int number = 0; int uglyNumber = 0; while (uglyNumber < index) { ++number; if(IsUgly (Number)) ++ uglyNumber; } return Number; } // ==================== algorithm code ========= 2 =========== // order to solve the next several ugly, take up additional space consumption int Min ( int number1, int number2, int number3); int GetUglyNumber_Solution2 ( int index) { IF ( index <= 0 ) return 0 ; int * = pUglyNumbers new new int [index]; pUglyNumbers [ 0 ] = . 1 ; // The first number is ugly 1 int nextUglyIndex = 1 ; // number of ugly found int * pMultiply2 = pUglyNumbers; // . Multiplied by the number of indexes ugly 2 multiplied by 2 ugly just greater than the current maximum number of int * = pUglyNumbers pMultiply3 ; // multiplied by the index number 2 is ugly. int * pMultiply5 = pUglyNumbers; // multiplied by the index number 5 of the ugly. the while (nextUglyIndex < index) { int min = min (pMultiply2 * * 2 , * * pMultiply3 . 3 , * * pMultiply5 . 5 ); // save the minimum value of the current index pUglyNumbers [nextUglyIndex] = min; the while (* * pMultiply22 <= pUglyNumbers [nextUglyIndex]) // update the current index, it is multiplied by 2 is just greater than the current maximum number of ugly ++ pMultiply2; the while (* * pMultiply3 . 3 <= pUglyNumbers [nextUglyIndex]) // update the current index ++ pMultiply3 ; the while (* * pMultiply5 . 5 <= pUglyNumbers [nextUglyIndex]) // update the current index ++ pMultiply5; ++ nextUglyIndex; } int Ugly = pUglyNumbers [nextUglyIndex - . 1 ]; // second bit is equal to the array index index index-1 the Delete [] pUglyNumbers; //Remember to delete the array created return Ugly; } int Min ( int number1, int number2, int number3) { int min = (number1 <number2)? Number1: number2; min = (min <number3)? Min: number3; return min ; }
// ====================测试代码==================== void Test(int index, int expected) { if (GetUglyNumber_Solution1(index) == expected) printf("solution1 passed\n"); else printf("solution1 failed\n"); if (GetUglyNumber_Solution2(index) == expected) printf("solution2 passed\n"); else printf("solution2 failed\n"); } int main(int argc, char* argv[]) { Test(1, 1); Test(2, 2); Test(3, 3); Test(4, 4); Test(5, 5); Test(6, 6); Test(7, 8); Test(8, 9); Test(9, 10); Test(10, 12); Test(11, 15); //Test(1500, 859963392); Test(0, 0); return 0; }
Analysis: space for time.
class Solution { public: int GetUglyNumber_Solution(int index) { if (index <= 0) return 0; int* pUglyNumbers = new int[index]; pUglyNumbers[0] = 1; int nextUglyIndex = 1; int* pMultiply2 = pUglyNumbers; int* pMultiply3 = pUglyNumbers; int* pMultiply5 = pUglyNumbers; while (nextUglyIndex < index) { int min = Min(*pMultiply2 * 2, *pMultiply3 * 3, *pMultiply5 * 5); pUglyNumbers[nextUglyIndex] = min; while(*pMultiply2 * 2 <= pUglyNumbers[nextUglyIndex]) ++pMultiply2; while(*pMultiply3 * 3 <= pUglyNumbers[nextUglyIndex]) ++pMultiply3; while(*pMultiply5 * 5 <= pUglyNumbers[nextUglyIndex]) ++pMultiply5; ++nextUglyIndex; } int ugly = pUglyNumbers[nextUglyIndex - 1]; delete[] pUglyNumbers; return ugly; } int Min(int number1, int number2, int number3) { int min = (number1 < number2) ? number1 : number2; min = (min < number3) ? min : number3; return min; } };