class Solution { public: int GetUglyNumber_Solution(int index) { int res = 0, i = 1,tmp; int arr[index]; int ptrto2 = 0, ptrto3 = 0, ptrto5 = 0; arr[0] = 1; while (i <= index - 1){ tmp = minOf3(arr[ptrto2]*2, arr[ptrto3]*3, arr[ptrto5]*5); if (tmp == arr[ptrto2]*2){ arr[i] = tmp; ptrto2 ++; } if (tmp == arr[ptrto3]*3){ arr[i] = tmp; ptrto3 ++; } if (tmp == arr[ptrto5]*5){ arr[i] = tmp; ptrto5 ++; } i++; } return arr[index - 1]; } int minOf3(int a, int b, int c){ int minzhi = 0; minzhi = a < b ? a : b ; minzhi = minzhi < c ? minzhi : c; return minzhi; } };
时间: 2024-10-14 00:35:54