Buckle 704. binary search (sequential search)

Buckle 704. binary search

https://leetcode-cn.com/problems/binary-search/

Given an n-element ordered (ascending) integer array nums and a target value target, write a function to search for the target in nums and return the subscript if the target value exists, otherwise it returns -1.

 

Idea: orderly, it is sequential search, the most convenient is binary search

My VS code:

#include "stdafx.h"
#include<vector>
using namespace std;
class Solution {
public:
	/**
	* @param nums: The integer array.
	* @param target: Target to find.
	* @return: The first position of target. Position starts from 0.
	*/
	int binarySearch(vector<int> &nums, int target) {
		// write your code here
		//有序数组,二分查找
		//二分查找也称为折半查找,每次都能将查找区间减半,这种折半特性的算法时间复杂度为 O(logN)
		if (nums.size()==0)
		{
			return -1;
		}
		int low = 0;
		int high = nums.size()-1;
		int mid = 0;
		while (low <= high)
		{
			mid = low+(high-low) / 2;
			if (target == nums[mid])
			{
				return mid;
			}
			else if(target > nums[mid])
			{
				low = mid+1;
			}
			else
			{
				high = mid-1;
			}
		}
		return -1;
	}
};

int main()
{
	Solution s;
	vector<int>nums;
	nums.push_back(1); nums.push_back(2); nums.push_back(3); nums.push_back(3); 
	nums.push_back(4); nums.push_back(5); nums.push_back(10); 
	auto result=s.binarySearch(nums,3);
	return 0;
}

Refer to the answer:

Method: Binary search
Binary search is a textbook algorithm based on comparing the target value and the middle element of the array.

If the target value is equal to the middle element, the target value is found.
If the target value is small, continue searching on the left.
If the target value is large, continue searching on the right.

Insert picture description here
algorithm:

Initialize the pointer left = 0, right = n-1.
When left <= right:
compare the middle element nums [pivot] with the target value target.
If target = nums [pivot], pivot is returned.
If target <nums [pivot], continue searching for right = pivot-1 on the left.
If target> nums [pivot], continue searching for left = pivot + 1 on the right.

Insert picture description here

Insert picture description here

class Solution {
  public:
  int search(vector<int>& nums, int target) {
    int pivot, left = 0, right = nums.size() - 1;
    while (left <= right) {
      pivot = left + (right - left) / 2;
      if (nums[pivot] == target) return pivot;
      if (target < nums[pivot]) right = pivot - 1;
      else left = pivot + 1;
    }
    return -1;
  }
};

作者:LeetCode
链接:https://leetcode-cn.com/problems/binary-search/solution/er-fen-cha-zhao-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Author: LeetCode
link: https: //leetcode-cn.com/problems/binary-search/solution/er-fen-cha-zhao-by-leetcode/
Source: stay button (LeetCode)
copyright reserved by the authors. For commercial reproduction, please contact the author for authorization, and for non-commercial reproduction, please indicate the source.

Published 23 original articles · praised 0 · visits 137

Guess you like

Origin blog.csdn.net/qq_35683407/article/details/105419439