Skip to content

Commit 403df82

Browse files
committed
feat: add rust solution to lc problem: No.0033
No.0033.Search in Rotated Sorted Array
1 parent dbdd6e3 commit 403df82

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

solution/0000-0099/0033.Search in Rotated Sorted Array/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,38 @@ var search = function (nums, target) {
168168
};
169169
```
170170

171+
### **Rust**
172+
173+
```rust
174+
impl Solution {
175+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
176+
let mut l = 0;
177+
let mut r = nums.len() - 1;
178+
while l <= r {
179+
let mid = l + r >> 1;
180+
if nums[mid] == target {
181+
return mid as i32;
182+
}
183+
184+
if nums[l] <= nums[mid] {
185+
if target < nums[mid] && target >= nums[l] {
186+
r = mid - 1;
187+
} else {
188+
l = mid + 1;
189+
}
190+
} else {
191+
if target > nums[mid] && target <= nums[r] {
192+
l = mid + 1;
193+
} else {
194+
r = mid - 1;
195+
}
196+
}
197+
}
198+
-1
199+
}
200+
}
201+
```
202+
171203
### **...**
172204

173205
```

solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md

+32
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,38 @@ var search = function (nums, target) {
144144
};
145145
```
146146

147+
### **Rust**
148+
149+
```rust
150+
impl Solution {
151+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
152+
let mut l = 0;
153+
let mut r = nums.len() - 1;
154+
while l <= r {
155+
let mid = l + r >> 1;
156+
if nums[mid] == target {
157+
return mid as i32;
158+
}
159+
160+
if nums[l] <= nums[mid] {
161+
if target < nums[mid] && target >= nums[l] {
162+
r = mid - 1;
163+
} else {
164+
l = mid + 1;
165+
}
166+
} else {
167+
if target > nums[mid] && target <= nums[r] {
168+
l = mid + 1;
169+
} else {
170+
r = mid - 1;
171+
}
172+
}
173+
}
174+
-1
175+
}
176+
}
177+
```
178+
147179
### **...**
148180

149181
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
impl Solution {
2+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
3+
let mut l = 0;
4+
let mut r = nums.len() - 1;
5+
while l <= r {
6+
let mid = l + r >> 1;
7+
if nums[mid] == target {
8+
return mid as i32;
9+
}
10+
11+
if nums[l] <= nums[mid] {
12+
if target < nums[mid] && target >= nums[l] {
13+
r = mid - 1;
14+
} else {
15+
l = mid + 1;
16+
}
17+
} else {
18+
if target > nums[mid] && target <= nums[r] {
19+
l = mid + 1;
20+
} else {
21+
r = mid - 1;
22+
}
23+
}
24+
}
25+
-1
26+
}
27+
}

0 commit comments

Comments
 (0)