1. 程式人生 > >[LeetCode]605. Can Place Flowers

[LeetCode]605. Can Place Flowers

給一個數組,只包含01,1不能相鄰,問能否插入n個1

前後都為0時,就將當前位置置為1,表示當前位置可以插入

public class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int cnt = 0;
        for (int i = 0; i < flowerbed.length && cnt < n; i++) {
            if (flowerbed[i] == 0) {
                int next = i == flowerbed.length - 1 ? 0 : flowerbed[i + 1];
                int prev = i == 0 ? 0 : flowerbed[i - 1];
                if (prev == 0 && next == 0) {
                    flowerbed[i] = 1;
                    cnt++;
                }
            }
        }
        return cnt == n;
    }
}


相關推薦

leetcode-605-Can Place Flowers

中間 ini violate 一位 開始 long not ann In 題目描述: Suppose you have a long flowerbed in which some of the plots are planted and some are not. Ho

leetcode:(605) Can Place Flowers(java)

題目:        Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot

[leetcode]-605. Can Place Flowers(C語言)

Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent

[LeetCode] 605. Can Place Flowers

題:https://leetcode.com/problems/can-place-flowers/description/ 題目 Suppose you have a long flowerbed in which some of the plots are planted and

【python3】leetcode 605. Can Place Flowers(easy)

605. Can Place Flowers(easy) Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers canno

[LeetCode]605. Can Place Flowers

給一個數組,只包含01,1不能相鄰,問能否插入n個1 前後都為0時,就將當前位置置為1,表示當前位置可以插入 public class Solution { public boolean canPlaceFlowers(int[] flowerbed,

leetcode -- 605. Can Place Flowers 【邊界處理 + 數學規律】

題目 Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adja

LeetCode 605. Can Place Flowers

Can Place Flowers 題目描述: Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cann

LeetCode#605: Can Place Flowers

Description Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be p

[leetcode]: 605. Can Place Flowers

1.題目 Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted

[leetcode]605. Can Place Flowers能放花嗎

不能 rule some cannot bool turn num input size Suppose you have a long flowerbed in which some of the plots are planted and some are not. H

605. Can Place FlowersLeetCode

bsp ive not ans for flow size plot suppose Suppose you have a long flowerbed in which some of the plots are planted and some are not. How

leetcode605. Can Place Flowers(Python & C++)

605. Can Place Flowers 題目連結 605.1 題目描述: Suppose you have a long flowerbed in which some of the plots are planted and some ar

605. Can Place Flowers

die com cnblogs public push_back tput range contain span Suppose you have a long flowerbed in which some of the plots are planted and som

leetcodeCan Place Flowers

Title:Can Place Flowers    605 Difficulty:Easy 原題leetcode地址:https://leetcode.com/problems/can-place-flowers/   1. 判斷當前位置的前一個數

C#LeetCode刷題之#605-種花問題( Can Place Flowers

問題 假設你有一個很長的花壇,一部分地塊種植了花,另一部分卻沒有。可是,花卉不能種植在相鄰的地塊上,它們會爭奪水源,兩者都會死去。 給定一個花壇(表示為一個數組包含0和1,其中0表示沒種植花,1表示種植了花),和一個數 n 。能否在不打破種植規則的情況下種入 n 朵花?能

Can Place Flowers

pan elif src spa log class object sel image     這道題為簡單題   題目:        思路:     我是這麽想的:能夠種植的條件有三種,其一、當前元素為0,左右兩邊也為0;其二、當該元素是列表第一個元素或者列表最後一

Leetcode605.Can Place Flowers種花問題

假設你有一個很長的花壇,一部分地塊種植了花,另一部分卻沒有。可是,花卉不能種植在相鄰的地塊上,它們會爭奪水源,兩者都會死去。 給定一個花壇(表示為一個數組包含0和1,其中0表示沒種植花,1表示種植了花),和一個數 n 。能否在不打破種植規則的情況下種入 n&nb

Can Place Flowers:陣列相鄰元素不為0的插入問題

Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjace

[Swift]LeetCode605. 種花問題 | Can Place Flowers

包含 rule which 數組 code solution long () ini Suppose you have a long flowerbed in which some of the plots are planted and some are not. How