跟进“搜索旋转排序数组”,假如有重复元素又将如何?
是否会影响运行时间复杂度?
如何影响?
为何会影响?
写出一个函数判断给定的目标值是否出现在数组中。
您在真实的面试中是否遇到过这个题?
Yes
样例
给出
[3,4,4,5,7,0,1,2]和target=4,返回 true
解题思路:
会有影响的,
public class Solution {
/**
* param A : an integer ratated sorted array and duplicates are allowed
* param target : an integer to be search
* return : a boolean
*/
public boolean search(int[] A, int target) {
// write your code here
for(int i=0;i<A.length;i++){
if(target==A[i]){
return true;
}
}
return false;
}
}