LeetCode 896. Monotonic Array

Problem Statement



impl Solution {
pub fn is_monotonic(a: Vec<i32>) -> bool {
if a.len() == 1 {
return true;
}

let mut flag = 0;
for i in 1..a.len() {
if a[i - 1] == a[i] {
continue;
}
let mut cur = 0;
if a[i - 1] < a[i] {
cur = 1;
} else {
cur = 2;
}
if flag != 0 && flag != cur {
return false;
}
flag = cur;
}
return true;
}
}