class Solution { public: int search(vector<int>& nums, int target) { return(helper(nums, target, 0, nums.size() - 1)); } int helper(vector<int>& nums, int &target, int left, int right) { if(left > right || nums[left] > target || nums[right] < target) //边界检测 return -1; if(nums[left] == target) //判断一个区间最左边的值是否为target return left; int mid = (left + right) / 2; //以中点分为两个区间 int l_i = helper(nums, target, left, mid); //递归得到左区间target的索引值 if(l_i != -1) //不为-1说明找到索引 return l_i; return helper(nums, target, mid + 1, right); //若左区间没找到则找右区间 } };