Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eff72ba

Browse files
authored
feat: add rust solution to lc problem: No.1095 (doocs#1794)
1 parent 5591807 commit eff72ba

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

‎solution/1000-1099/1095.Find in Mountain Array/README.md‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,58 @@ public:
213213
};
214214
```
215215
216+
### **Rust**
217+
218+
```rust
219+
impl Solution {
220+
#[allow(dead_code)]
221+
pub fn find_in_mountain_array(target: i32, mountain_arr: &MountainArray) -> i32 {
222+
let n = mountain_arr.length();
223+
224+
// First find the maximum element in the array
225+
let mut l = 0;
226+
let mut r = n - 1;
227+
228+
while l < r {
229+
let mid = (l + r) >> 1;
230+
if mountain_arr.get(mid) > mountain_arr.get(mid + 1) {
231+
r = mid;
232+
} else {
233+
l = mid + 1;
234+
}
235+
}
236+
237+
let left = Self::binary_search(mountain_arr, 0, l, 1, target);
238+
239+
if left == -1 {
240+
Self::binary_search(mountain_arr, l, n - 1, -1, target)
241+
} else {
242+
left
243+
}
244+
}
245+
246+
#[allow(dead_code)]
247+
fn binary_search(m: &MountainArray, mut l: i32, mut r: i32, k: i32, target: i32) -> i32 {
248+
let n = m.length();
249+
250+
while l < r {
251+
let mid = (l + r) >> 1;
252+
if k * m.get(mid) >= k * target {
253+
r = mid;
254+
} else {
255+
l = mid + 1;
256+
}
257+
}
258+
259+
if m.get(l) == target {
260+
l
261+
} else {
262+
-1
263+
}
264+
}
265+
}
266+
```
267+
216268
### **Go**
217269

218270
```go

‎solution/1000-1099/1095.Find in Mountain Array/README_EN.md‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,58 @@ public:
183183
};
184184
```
185185
186+
### **Rust**
187+
188+
```rust
189+
impl Solution {
190+
#[allow(dead_code)]
191+
pub fn find_in_mountain_array(target: i32, mountain_arr: &MountainArray) -> i32 {
192+
let n = mountain_arr.length();
193+
194+
// First find the maximum element in the array
195+
let mut l = 0;
196+
let mut r = n - 1;
197+
198+
while l < r {
199+
let mid = (l + r) >> 1;
200+
if mountain_arr.get(mid) > mountain_arr.get(mid + 1) {
201+
r = mid;
202+
} else {
203+
l = mid + 1;
204+
}
205+
}
206+
207+
let left = Self::binary_search(mountain_arr, 0, l, 1, target);
208+
209+
if left == -1 {
210+
Self::binary_search(mountain_arr, l, n - 1, -1, target)
211+
} else {
212+
left
213+
}
214+
}
215+
216+
#[allow(dead_code)]
217+
fn binary_search(m: &MountainArray, mut l: i32, mut r: i32, k: i32, target: i32) -> i32 {
218+
let n = m.length();
219+
220+
while l < r {
221+
let mid = (l + r) >> 1;
222+
if k * m.get(mid) >= k * target {
223+
r = mid;
224+
} else {
225+
l = mid + 1;
226+
}
227+
}
228+
229+
if m.get(l) == target {
230+
l
231+
} else {
232+
-1
233+
}
234+
}
235+
}
236+
```
237+
186238
### **Go**
187239

188240
```go
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn find_in_mountain_array(target: i32, mountain_arr: &MountainArray) -> i32 {
4+
let n = mountain_arr.length();
5+
6+
// First find the maximum element in the array
7+
let mut l = 0;
8+
let mut r = n - 1;
9+
10+
while l < r {
11+
let mid = (l + r) >> 1;
12+
if mountain_arr.get(mid) > mountain_arr.get(mid + 1) {
13+
r = mid;
14+
} else {
15+
l = mid + 1;
16+
}
17+
}
18+
19+
let left = Self::binary_search(mountain_arr, 0, l, 1, target);
20+
21+
if left == -1 {
22+
Self::binary_search(mountain_arr, l, n - 1, -1, target)
23+
} else {
24+
left
25+
}
26+
}
27+
28+
#[allow(dead_code)]
29+
fn binary_search(m: &MountainArray, mut l: i32, mut r: i32, k: i32, target: i32) -> i32 {
30+
let n = m.length();
31+
32+
while l < r {
33+
let mid = (l + r) >> 1;
34+
if k * m.get(mid) >= k * target {
35+
r = mid;
36+
} else {
37+
l = mid + 1;
38+
}
39+
}
40+
41+
if m.get(l) == target {
42+
l
43+
} else {
44+
-1
45+
}
46+
}
47+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /