1- 思路
二分
- 左区间二分找分界点,二分找到旋转后的分界点即可
- 以
nums[mid]
为基准,对比 nums[0]
即可找到区间分界点
2- 实现
⭐33. 搜索旋转排序数组——题解思路
class Solution {public int findMin(int[] nums) {int left = 0;int right = nums.length-1;while(left<right){int mid = left + (right - left) /2+1;if(nums[mid] >= nums[0]){left = mid;}else{right = mid-1;}}if(right == nums.length-1) return nums[0];return nums[right+1];}
}
3- ACM 实现
public class findMin {public static int binaryS(int[] nums){int left= 0;int right = nums.length-1;while(left<right){int mid = (left+right)/2+1;if(nums[mid] >= nums[0]){left = mid;}else {right = mid-1;}}if(right == nums.length-1) return nums[0];return nums[right+1];}public static void main(String[] args) {Scanner sc = new Scanner(System.in);String input = sc.nextLine();input = input.replace("[","").replace("]","");String[] parts = input.split(",");int[] nums = new int[parts.length];for(int i = 0 ; i < nums.length; i++){nums[i] = Integer.parseInt(parts[i]);}System.out.println("结果是"+binaryS(nums));}
}