题解 | #旋转数组的最小数字#
旋转数组的最小数字
https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba
一行代码搞定!
import java.util.*; public class Solution { public int minNumberInRotateArray(int [] array) { return Arrays.stream(array).min().getAsInt(); } }上面那个时间和空间都快逼近爆了哈哈,作死的边缘反复横跳,还是二分才是正解!
import java.util.*; public class Solution { public int minNumberInRotateArray(int [] array) { int left = 0,mid = 0; int right = array.length - 1; while(left < right){ mid = (left + right) / 2; if(array[mid] > array[right]) left = mid + 1; else if(array[mid] < array[right]) right = mid; else right--; } return array[left]; } }