1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
fn main() { println!("Hello, world!"); let tests = [ (vec![1,2,2,3],true), (vec![6,5,4,4],true), (vec![1,3,2], false) ]; for test in tests { println!("{:?} is {} should be {}", test.0, Solution::is_monotonic(test.0.clone()), test.1); } } struct Solution; impl Solution { pub fn is_monotonic(nums: Vec<i32>) -> bool { nums.windows(2).all(|x| x[0] >= x[1]) || nums.windows(2).all(|x| x[0] <= x[1]) } }