Re: [闲聊] 每日LeetCode

楼主: yam276 ('_')   2023-09-30 00:16:45
896. 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
}
}
作者: smart0eddie (smart0eddie)   2023-09-30 00:32:00
不然你原本打算怎么做
楼主: yam276 ('_')   2023-09-30 00:33:00
我就烂我本来只有用一个变量算递增递减趋势用两个就省很多时间

Links booklink

Contact Us: admin [ a t ] ucptt.com