leetcode 二分查找 Search in Rotated Sorted ArrayII

Search in Rotated Sorted Array II

Total Accepted: 18500 Total
Submissions: 59945My Submissions

Follow up for "Search in Rotated Sorted Array":

What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Write a function to determine if a given target is in the array.

题意:一个已经排序好的数组,被按某个位置旋转了一次,给定一个值target,在该旋转后的数组里查找该值。(数组中的元素可能重复)

思路:二分查找

难点在于确定往数组的哪一半段继续二分查找

设起点、中间点、终点分别为 start、middle、end (采用前闭后开的区间表示方法

如果target = A[middle] return middle

如果A[middle] > A[start],则[start,middle)单调递增

1.如果target < A[middle] && target >= A[start],则 end = middle

2.start = middle + 1, otherwise

如果A[middle] < A[start],则[middle,end)单调递增

1.如果target > A[middle] && target <= A[end - 1],则 start = middle + 1

2.end = middle, otherwise

如果A[middle] == A[start],那A[start]也不会是target,可以通过start++; 去掉A[start]

复杂度:时间O(n),空间O(1)

int search(int A[], int n, int target){
	int start = 0, end = n, middle ;
	while(start < end){
		middle = (start + end) / 2;
		if(A[middle] == target) return middle;
		if(A[middle] > A[start]){
			if(target >= A[start] && target < A[middle]){
				end = middle;
			}else{
				start = middle + 1;
			}
		}else if(A[middle] < A[start]){
			if(target > A[middle] && target <= A[end - 1]){
				start = middle + 1;
			}else{
				end = middle;
			}
		}
		else{
			++start;
		}
	}
	return -1;
}
时间: 2024-07-31 22:46:15

leetcode 二分查找 Search in Rotated Sorted ArrayII的相关文章

leetcode 二分查找 Search in Rotated Sorted Array

Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose a sorted array 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 s

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array 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 du

Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码: class Solution { public: int search(vector<int>& nums, int target) { int numsSize = nums.size(); int low = 0,high = numsSize; while(low < hi

Leetcode 二分查找 Search a 2D Matrix

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search a 2D Matrix Total Accepted: 10871 Total Submissions: 35629 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in e

LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 这个和前面的一个不一样就在于可能会有重复的数字,那么判断的时候就应该注意了,遇到start

[Leetcode][Python]33: Search in Rotated Sorted Array

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 33: Search in Rotated Sorted Arrayhttps://oj.leetcode.com/problems/search-in-rotated-sorted-array/ Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6

【LeetCode】81. Search in Rotated Sorted Array II

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 这题跟上一题一样,都是二分查找法,但是需要思考,对于时间复杂度,存在重复元素会有影

leetcode || 81、Search in Rotated Sorted Array II

problem: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. Hide Tags Array Binary Search 题意:对于