605. Can Place Flowers种花问题【leetcode】

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 plots - they would compete for water and both would die.

Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if nnew flowers can be planted in it without violating the no-adjacent-flowers rule.

Example 1:

Input: flowerbed = [1,0,0,0,1], n = 1
Output: True

Example 2:

Input: flowerbed = [1,0,0,0,1], n = 2
Output: False

Note:

  1. The input array won‘t violate no-adjacent-flowers rule.
  2. The input array size is in the range of [1, 20000].
  3. n is a non-negative integer which won‘t exceed the input array size.

枚举法写出来 答案,计算可以种花总数是否比n大即可,有投机的成分

//如果这个东西是奇偶数,如果里面的客房偶数比b大即可
/*
左为0 当前为0 且右边为0或边界 这时改为1 count++
左为0 当前为1 继续
左为1 当前为0 继续
左为1 当前为1 继续
*/
public class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int count =0;
        int len=flowerbed.length;

        if(len==1&&flowerbed[0]==0){
                count++;
        }
        if(len==2&&(flowerbed[0]==0&&flowerbed[1]==0)){
                count++;
        }
        for(int i=1;len>1&&i<len-1;i++){
            if(i==1&&flowerbed[i]==0&&flowerbed[i-1]==0){
                flowerbed[i-1] =1;
                count++;
            }
            if((flowerbed[i]==0)&&(flowerbed[i]==flowerbed[i-1])&&(flowerbed[i]==flowerbed[i+1])){
                flowerbed[i]=1;
                count++;
            }
            if(i==len-2&&flowerbed[i]==0&&flowerbed[i+1]==0){
                flowerbed[i+1] =1;
                count++;
            }
        }
        if(count>=n)
        {return true;}
        return false;
    }
}

第二种办法,根据一次写的边界值进行归类

因为只需要判断当前元素如果是0的时候与左、右边界值(如果存在着)进行对比

//如果这个东西是奇偶数,如果里面的客房偶数比b大即可
/*
左为0 当前为0 且右边为0或边界 这时改为1 count++
左为0 当前为1 继续
左为1 当前为0 继续
左为1 当前为1 继续
*/
public class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int count =0;
        int len=flowerbed.length;
        //boolean flag =flase;
        int right=-1;
        int left =-1;
        for(int i=0;i<len;i++){
            //判断当前是否为0,如果为0,且右边有值则比较 否则直接改
            int mid=flowerbed[i];

            if(mid==0){
                //右边界
                if(i==0){
                    if(i+1<len){
                        right =flowerbed[i+1];
                        if(mid==right){
                        flowerbed[i]=1;
                        count++;
                    }

                        }
                    else {
                        flowerbed[i]=1;
                        count++;
                    }

                }
                //右边界
                if(i==len-1){
                    if(i-1>=0){
                        left =flowerbed[i-1];
                        if(mid==left){
                            flowerbed[i]=1;
                            count++;
                        }
                    }
                    else {
                        flowerbed[i]=1;
                        count++;
                    }
                }
                //中间值
                if(i-1>=0&&i+1<len){
                     left =flowerbed[i-1];
                     right =flowerbed[i+1];
                    if(left==mid&&mid==right){
                        flowerbed[i]=1;
                        count++;
                    }
                }

            }

        }
        if(count>=n)
        {return true;}
        return false;
    }
}

官方答案1

public class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int i = 0, count = 0;
        while (i < flowerbed.length) {
            if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == flowerbed.length - 1 || flowerbed[i + 1] == 0)) {
                flowerbed[i++] = 1;
                count++;
            }
             if(count>=n)
                return true;
            i++;
        }
        return false;
    }
}

T :o(n) Space:O(1)

精简的写法

public class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int count =0;
        int len=flowerbed.length;

        for(int i=0;i<len;i++){
            //判断当前是否为0,如果为0,且左右相邻均为0则可以种花,种花后修改花盆属性和种花数量
            if(flowerbed[i]==0){
                int right=(i==len-1)?0:flowerbed[i+1];
                int left=(i==0)?0:flowerbed[i-1];
                if(left==right&&right==0){
                    flowerbed[i]=1;
                    count++;
                }
            }
            if(count>=n){
                return true;
            }
        }

        return false;
    }
}
时间: 2024-10-11 10:39:38

605. Can Place Flowers种花问题【leetcode】的相关文章

Leetcode - 605 - Can Place Flowers

Leetcode - 605 - Can Place Flowers 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 adjacent plots - they would compete for water and both would di

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 cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represe

[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 adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array containing 0

605. Can Place Flowers(LeetCode)

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 plots - they would compete for water and both would die. Given a flowerbed (represented as an array containing 0

【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 planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array

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 adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array containing 0

LeetCode第四天

leetcode 第四天 2018年1月4日 15.(628)Maximum Product of Three Numbers JAVA class Solution { public int maximumProduct(int[] nums) { Arrays.sort(nums); return (nums[0]*nums[1]*nums[nums.length-1])>(nums[nums.length-1]*nums[nums.length-2]*nums[nums.length-3]

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482