【LeetCode 33】Search in Rotated Sorted Array
题意: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. Your algorithm’s runtime complexity must be in the order of O(log n). 思路: 想到了二分,但没想到,关键是,可以根据mid和nums[l]和nums[r]的大小关系,判断出一个单调区间,然后根据target和这个区间两端值得大小关系,判断是否在这一边,决定保留哪一边。 代码: class Solution { public : int search ( vector < int > & nums , int target ) { int l = 0 ; int r = nums . size ( ) - 1 ; if