Skip to content

Commit e35725e

Browse files
committed
feat: add rust solution to lc problem: No.0034
No.0034.Find First and Last Position of Element in Sorted Array
1 parent 974948b commit e35725e

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,56 @@ func searchRange(nums []int, target int) []int {
233233
}
234234
```
235235

236+
### **Rust**
237+
238+
```rust
239+
use std::cmp::Ordering;
240+
241+
impl Solution {
242+
pub fn search_range(nums: Vec<i32>, target: i32) -> Vec<i32> {
243+
let n = nums.len();
244+
let mut l = 0;
245+
let mut r = n;
246+
while l < r {
247+
let mid = l + (r - l) / 2;
248+
match nums[mid].cmp(&target) {
249+
Ordering::Less => l = mid + 1,
250+
Ordering::Greater => r = mid,
251+
Ordering::Equal => {
252+
let mut res = vec![mid as i32, mid as i32];
253+
let mut t = mid;
254+
while l < t {
255+
let mid = l + (t - l) / 2;
256+
match nums[mid].cmp(&target) {
257+
Ordering::Less => l = mid + 1,
258+
Ordering::Greater => t = mid,
259+
Ordering::Equal => {
260+
res[0] = mid as i32;
261+
t = mid;
262+
}
263+
}
264+
}
265+
t = mid + 1;
266+
while t < r {
267+
let mid = t + (r - t) / 2;
268+
match nums[mid].cmp(&target) {
269+
Ordering::Less => t = mid + 1,
270+
Ordering::Greater => r = mid,
271+
Ordering::Equal => {
272+
res[1] = mid as i32;
273+
t = mid + 1;
274+
}
275+
}
276+
}
277+
return res;
278+
}
279+
}
280+
}
281+
vec![-1, -1]
282+
}
283+
}
284+
```
285+
236286
### **...**
237287

238288
```

solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README_EN.md

+50
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,56 @@ func searchRange(nums []int, target int) []int {
209209
}
210210
```
211211

212+
### **Rust**
213+
214+
```rust
215+
use std::cmp::Ordering;
216+
217+
impl Solution {
218+
pub fn search_range(nums: Vec<i32>, target: i32) -> Vec<i32> {
219+
let n = nums.len();
220+
let mut l = 0;
221+
let mut r = n;
222+
while l < r {
223+
let mid = l + (r - l) / 2;
224+
match nums[mid].cmp(&target) {
225+
Ordering::Less => l = mid + 1,
226+
Ordering::Greater => r = mid,
227+
Ordering::Equal => {
228+
let mut res = vec![mid as i32, mid as i32];
229+
let mut t = mid;
230+
while l < t {
231+
let mid = l + (t - l) / 2;
232+
match nums[mid].cmp(&target) {
233+
Ordering::Less => l = mid + 1,
234+
Ordering::Greater => t = mid,
235+
Ordering::Equal => {
236+
res[0] = mid as i32;
237+
t = mid;
238+
}
239+
}
240+
}
241+
t = mid + 1;
242+
while t < r {
243+
let mid = t + (r - t) / 2;
244+
match nums[mid].cmp(&target) {
245+
Ordering::Less => t = mid + 1,
246+
Ordering::Greater => r = mid,
247+
Ordering::Equal => {
248+
res[1] = mid as i32;
249+
t = mid + 1;
250+
}
251+
}
252+
}
253+
return res;
254+
}
255+
}
256+
}
257+
vec![-1, -1]
258+
}
259+
}
260+
```
261+
212262
### **...**
213263

214264
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::cmp::Ordering;
2+
3+
impl Solution {
4+
pub fn search_range(nums: Vec<i32>, target: i32) -> Vec<i32> {
5+
let n = nums.len();
6+
let mut l = 0;
7+
let mut r = n;
8+
while l < r {
9+
let mid = l + (r - l) / 2;
10+
match nums[mid].cmp(&target) {
11+
Ordering::Less => l = mid + 1,
12+
Ordering::Greater => r = mid,
13+
Ordering::Equal => {
14+
let mut res = vec![mid as i32, mid as i32];
15+
let mut t = mid;
16+
while l < t {
17+
let mid = l + (t - l) / 2;
18+
match nums[mid].cmp(&target) {
19+
Ordering::Less => l = mid + 1,
20+
Ordering::Greater => t = mid,
21+
Ordering::Equal => {
22+
res[0] = mid as i32;
23+
t = mid;
24+
}
25+
}
26+
}
27+
t = mid + 1;
28+
while t < r {
29+
let mid = t + (r - t) / 2;
30+
match nums[mid].cmp(&target) {
31+
Ordering::Less => t = mid + 1,
32+
Ordering::Greater => r = mid,
33+
Ordering::Equal => {
34+
res[1] = mid as i32;
35+
t = mid + 1;
36+
}
37+
}
38+
}
39+
return res;
40+
}
41+
}
42+
}
43+
vec![-1, -1]
44+
}
45+
}

0 commit comments

Comments
 (0)