标签:cto lint SM lan search 重复 资源 html tor
原题网址:https://www.lintcode.com/problem/search-in-rotated-sorted-array-ii/description
跟进“搜索旋转排序数组”,假如有重复元素又将如何?
是否会影响运行时间复杂度?
如何影响?
为何会影响?
写出一个函数判断给定的目标值是否出现在数组中。
给出[3,4,4,5,7,0,1,2]和target=4,返回 true
class Solution { public: /** * @param A: an integer ratated sorted array and duplicates are allowed * @param target: An integer * @return: a boolean */ bool search(vector<int> &A, int target) { // write your code here if (A.empty()) { return false; } int size=A.size(); int left=0,right=size-1,mid; while(left<=right) { mid=(left+right)/2; if (A[mid]==target) { return true; } if (A[mid]>A[left]) { if (A[left]<=target&&target<A[mid]) { right=mid-1; } else { left=mid+1; } } else if (A[mid]<A[left]) if (target>A[mid]&&target<=A[right]) { left=mid+1; } else { right=mid-1; } } else//相等时顺序查找; { left++; } } return false; } };
标签:cto lint SM lan search 重复 资源 html tor
原文地址:https://www.cnblogs.com/Tang-tangt/p/9126892.html