class Solution {
public:
bool search(int A[], int n, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
for (int i = 0; i < n; i++)
if (A[i] == target)
return true;
return false;
}
};
Small Case: 12ms
Large Case: 44ms
Time: O(n)
Space: O(1)
当有重复元素时,无法在O(lgn)的时间内找到。比如有1个0和n-1个1,无论怎么位移和查找,都至少需要O(n)才能确定0的位置。