diff --git a/notes/11. 旋转数组的最小数字.md b/notes/11. 旋转数组的最小数字.md index 020db3ff..31ce212e 100644 --- a/notes/11. 旋转数组的最小数字.md +++ b/notes/11. 旋转数组的最小数字.md @@ -2,7 +2,7 @@ ## 题目链接 -[牛客网](https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba?tpId=13&tqId=11159&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github) +[Leetcode](https://leetcode.cn/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/) ## 题目描述 @@ -24,38 +24,38 @@ - 否则 [m + 1, h] 区间内的数组是旋转数组,令 l = m + 1。 ```java -public int minNumberInRotateArray(int[] nums) { - if (nums.length == 0) +public int inventoryManagement(int[] stock) { + if (stock.length == 0) return 0; - int l = 0, h = nums.length - 1; + int l = 0, h = stock.length - 1; while (l < h) { int m = l + (h - l) / 2; - if (nums[m] <= nums[h]) + if (stock[m] <= stock[h]) h = m; else l = m + 1; } - return nums[l]; + return stock[l]; } ``` 如果数组元素允许重复,会出现一个特殊的情况:nums[l] == nums[m] == nums[h],此时无法确定解在哪个区间,需要切换到顺序查找。例如对于数组 {1,1,1,0,1},l、m 和 h 指向的数都为 1,此时无法知道最小数字 0 在哪个区间。 ```java -public int minNumberInRotateArray(int[] nums) { - if (nums.length == 0) +public int inventoryManagement(int[] stock) { + if (stock.length == 0) return 0; - int l = 0, h = nums.length - 1; + int l = 0, h = stock.length - 1; while (l < h) { int m = l + (h - l) / 2; - if (nums[l] == nums[m] && nums[m] == nums[h]) - return minNumber(nums, l, h); - else if (nums[m] <= nums[h]) + if (stock[l] == stock[m] && stock[m] == stock[h]) + return minNumber(stock, l, h); + else if (stock[m] <= stock[h]) h = m; else l = m + 1; } - return nums[l]; + return stock[l]; } private int minNumber(int[] nums, int l, int h) {