-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid-mountain-array.rs
40 lines (36 loc) · 992 Bytes
/
valid-mountain-array.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
struct Solution;
impl Solution {
pub fn valid_mountain_array(a: Vec<i32>) -> bool {
let length = a.len();
if length < 3 || a[0] > a[1] || a[length - 1] > a[length - 2] {
return false;
}
let mut find = false;
for (i, v) in a[..length - 1].iter().enumerate() {
let next_value = &a[i + 1];
if v == next_value {
return false;
}
if !find && v > next_value {
find = true
}
if find && v < next_value {
return false;
}
}
true
}
}
fn main() {
println!("{}", Solution::valid_mountain_array(vec![2, 1]));
}
#[cfg(test)]
mod tests {
use crate::Solution;
#[test]
fn test() {
assert!(!Solution::valid_mountain_array(vec![2, 1]));
assert!(!Solution::valid_mountain_array(vec![3, 5, 5]));
assert!(Solution::valid_mountain_array(vec![0, 3, 2, 1]));
}
}