[闲聊] 每日leetcode 75 - Day3

楼主: yam276 ('_')   2025-05-29 14:54:10
605. Can Place Flowers
https://leetcode.com/problems/can-place-flowers/
题意:
简单题 小便斗问题
男人只能在左右没人的小便斗上厕所
现在给你一个小便斗阵列
0 代表没被占用 1 代表有人在尿尿
问你能不能让 n 个刚进厕所的男人都找到左右没人的小便斗
思路:
先在阵列前后插入 0
然后 for 从 1 到 新阵列 len-1
就能不考虑边界问题
Code:
impl Solution {
pub fn can_place_flowers(flowerbed: Vec<i32>, mut n: i32) -> bool {
let mut pedded = vec![0];
pedded.extend(flowerbed);
pedded.push(0);
for i in 1..pedded.len() - 1 {
if pedded[i] == 0 && pedded[i - 1] == 0 && pedded[i + 1] == 0 {
pedded[i] = 1;
n -= 1;
}
if n <= 0 {
return true;
}
}
false
}
}
作者: DJYOMIYAHINA (通通打死)   2025-05-29 14:56:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com