描述
跟进“搜索旋转排序数组”,假如有重复元素又将如何?
写出一个函数判断给定的目标值是否出现在数组中。
样例
给出[3,4,4,5,7,0,1,2]和target=4,返回 true
思考
- 直接 for 循环查找
代码
// By Lentitude
class Solution {
/**
* param A : an integer ratated sorted array and duplicates are allowed
* param target : an integer to be search
* return : a boolean
*/
public:
bool search(vector<int> &A, int target) {
// write your code here
int len = A.size();
for (int i = 0; i != len; ++i){
if (A[i] == target)
return true;
}
return false;
}
};