LeetCode704. 二分查找
- 题目链接
- 代码
题目链接
https://leetcode.cn/problems/binary-search/description/
代码
class Solution {
public:int search(vector<int>& nums, int target) {int left = 0;int right = nums.size() - 1;while(left <= right){int middle = left + (right - left) / 2;if(nums[middle] > target){right = middle - 1;}else if(nums[middle] < target){left = middle + 1;}else{return middle;}}return -1;}
};