forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch_in_Rotated_Sorted_Array_II.cpp
More file actions
43 lines (39 loc) · 1.03 KB
/
Search_in_Rotated_Sorted_Array_II.cpp
File metadata and controls
43 lines (39 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 17, 2012
Problem: Search in Rotated Sorted Array II
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
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.
Solution:
If the duplicates are allowed, the binary search is no longer feasible.
For example,
1 1 1 3 1, and search for 3
target > A[mid] and we compare A[mid] and A[right].
We found A[mid] == A[right], but 3 can appear at both sides.
Thus I think the time timplexity is O(n) in the worst case.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
bool search(int A[], int n, int target) {
for (int i = 0; i < n; i++) {
if (A[i] == target) {
return true;
}
}
return false;
}
};