-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_in_rotated_sorted_array_ii.rs
72 lines (63 loc) · 1.94 KB
/
search_in_rotated_sorted_array_ii.rs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
///
/// Problem: Search in Rotated Sorted Array II
///
/// You are given an integer array `nums` sorted in **ascending order** and rotated at some pivot index.
///
/// Given a target value `target`, return **`true` if `target` exists in `nums`, otherwise return `false`.**
///
/// **Unlike `Search in Rotated Sorted Array I`, this array may contain **duplicates**.**
///
/// **Example 1:**
/// ```plaintext
/// Input: nums = [2,5,6,0,0,1,2], target = 0
/// Output: true
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: nums = [2,5,6,0,0,1,2], target = 3
/// Output: false
/// ```
///
/// **Constraints:**
/// - `1 <= nums.length <= 5000`
/// - `-10^4 <= nums[i] <= 10^4`
/// - `nums` is sorted and rotated at an unknown pivot.`
///
/// # Solution:
/// - **Time Complexity:** `O(log n)`
/// - **Space Complexity:** `O(1)`
impl Solution {
pub fn search(nums: Vec<i32>, target: i32) -> bool {
if nums.is_empty() {
return false;
}
let (mut left, mut right) = (0, nums.len() as isize - 1);
while left <= right {
let mid = left + (right - left) / 2;
if nums[mid as usize] == target {
return true;
}
if nums[left as usize] == nums[mid as usize] && nums[mid as usize] == nums[right as usize] {
left += 1;
right -= 1;
continue;
}
if nums[left as usize] <= nums[mid as usize] {
if nums[left as usize] <= target && target < nums[mid as usize] {
right = mid - 1;
} else {
left = mid + 1;
}
}
else {
if nums[mid as usize] < target && target <= nums[right as usize] {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
false
}
}