楼主:
yam276 ('_')
2023-09-30 00:16:45896. Monotonic Array
判断输入的阵列是否为递减或递增
思路:
看到别人以下的简洁解法我破防了
建立is递增跟is递减的bool变量为true
从1开始跑for 如果是递增数列 is递减就为false
如果是递减数列 is递增就为false
return is递增 || is递减
Code:
impl Solution {
pub fn is_monotonic(nums: Vec<i32>) -> bool {
let mut is_increasing = true;
let mut is_decreasing = true;
for index in 1..nums.len() {
if nums[index] > nums[index - 1] {
is_decreasing = false;
}
if nums[index] < nums[index - 1] {
is_increasing = false;
}
}
is_increasing || is_decreasing
}
}