TOP101题解 | BM21#旋转数组的最小数字#
旋转数组的最小数字
https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba
/** * @author Senky * @date 2023.04.26 * @par url https://www.nowcoder.com/creation/manager/content/584337070?type=column&status=-1 * @brief O(n)直接把数组排序,输出最小的元素;时间复杂度为O(nlogN),则用快排; * @param rotateArray int整型一维数组 * @param rotateArrayLen int rotateArray数组长度 * @return int整型 */ #include <stdlib.h> int minNumberInRotateArray(int* rotateArray, int rotateArrayLen ) { // write code here int left = 0; int right = rotateArrayLen - 1; int mid = 0; while(left <right) { mid = (left + right) /2; if(rotateArray[mid] > rotateArray[right]) { left = mid + 1; } else if(rotateArray[mid] < rotateArray[right]) { right = mid; } else if(rotateArray[mid] == rotateArray[right]) { right--; } } return rotateArray[left]; }
/** * @author Senky * @date 2023.04.26 * @par url https://www.nowcoder.com/creation/manager/content/584337070?type=column&status=-1 * @brief O(n)直接把数组排序,输出最小的元素;时间复杂度为O(nlogN),则用快排; * @param rotateArray int整型一维数组 * @param rotateArrayLen int rotateArray数组长度 * @return int整型 */ #include <stdlib.h> int compar(const void* p1, const void* p2) { return (*(int*)p1)- (*(int*)p2); } int minNumberInRotateArray(int* rotateArray, int rotateArrayLen ) { // write code here qsort(rotateArray, rotateArrayLen, sizeof(rotateArray[0]), compar); return rotateArray[0]; }#TOP101#
TOP101-BM系列 文章被收录于专栏
系列的题解