精華區beta Marginalman 關於我們 聯絡資訊
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 } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.32.48.170 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1748501653.A.F9B.html
DJYOMIYAHINA: 大師 05/29 14:56