Skip to content

Commit eb4c74e

Browse files
committed
LC 33. Search in Rotated Sorted Array (Rust BS)
1 parent 311b50b commit eb4c74e

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
4444
| [27. Remove Element][lc27] | 🟢 Easy | [![python](res/py.png)][lc27py] |
4545
| [28. Find the Index of the First Occurrence in a String][lc28] | 🟠 Medium | [![python](res/py.png)][lc28py] [![rust](res/rs.png)][lc28rs] |
4646
| [29. Divide two integers][lc29] | 🟠 Medium | [![python](res/py.png)][lc29py] |
47-
| [33. Search in Rotated Sorted Array][lc33] | 🟠 Medium | [![python](res/py.png)][lc33py] |
47+
| [33. Search in Rotated Sorted Array][lc33] | 🟠 Medium | [![python](res/py.png)][lc33py] [![rust](res/rs.png)][lc33rs] |
4848
| [34. Find First and Last Position of Element in Sorted Array][lc34] | 🟠 Medium | [![python](res/py.png)][lc34py] |
4949
| [35. Search Insert Position][lc35] | 🟢 Easy | [![python](res/py.png)][lc35py] [![rust](res/rs.png)][lc35rs] |
5050
| [36. Valid Sudoku][lc36] | 🟠 Medium | [![python](res/py.png)][lc36py] |
@@ -647,6 +647,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
647647
[lc29py]: leetcode/divide-two-integers.py
648648
[lc33]: https://leetcode.com/problems/search-in-rotated-sorted-array/
649649
[lc33py]: leetcode/search-in-rotated-sorted-array.py
650+
[lc33rs]: leetcode/search-in-rotated-sorted-array.rs
650651
[lc34]: https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
651652
[lc34py]: leetcode/find-first-and-last-position-of-element-in-sorted-array.py
652653
[lc35]: https://leetcode.com/problems/search-insert-position/
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// 33. Search in Rotated Sorted Array
2+
// 🟠 Medium
3+
//
4+
// https://leetcode.com/problems/search-in-rotated-sorted-array/
5+
//
6+
// Tags: Array - Binary Search
7+
8+
struct Solution;
9+
impl Solution {
10+
/// Neat idea and explanation found here:
11+
/// https://leetcode.com/problems/search-in-rotated-sorted-array/solutions/14435/clever-idea-making-it-simple/
12+
///
13+
/// Time complexity: O(log(n)) - The algorithm works like a binary search
14+
/// with an extra if statement executed in each iteration.
15+
/// Space complexity: O(1) - We use constant extra space.
16+
///
17+
/// Runtime 1 ms Beats 68.47%
18+
/// Memory 2 MB Beats 79.73%
19+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
20+
let (mut l, mut r) = (0, nums.len());
21+
let mut num;
22+
let mut mid;
23+
while l < r {
24+
mid = (l + r) / 2;
25+
num = if (nums[mid] < nums[0]) == (target < nums[0]) {
26+
nums[mid]
27+
} else {
28+
if target < nums[0] {
29+
i32::MIN
30+
} else {
31+
i32::MAX
32+
}
33+
};
34+
if num < target {
35+
l = mid + 1;
36+
} else if num > target {
37+
r = mid;
38+
} else {
39+
return mid as i32;
40+
}
41+
}
42+
-1
43+
}
44+
}
45+
46+
// Tests.
47+
fn main() {
48+
let tests = [
49+
(vec![1], 0, -1),
50+
(vec![4, 5, 6, 7, 0, 1, 2], 0, 4),
51+
(vec![4, 5, 6, 7, 0, 1, 2], 3, -1),
52+
];
53+
for t in tests {
54+
assert_eq!(Solution::search(t.0, t.1), t.2);
55+
}
56+
println!("\x1b[92m» All tests passed!\x1b[0m");
57+
}

0 commit comments

Comments
 (0)