其他分享
首页 > 其他分享> > Leetcode605.种花问题

Leetcode605.种花问题

作者:互联网

题目链接:605.种花问题

思路:题解有点没认真看,不知道是啥意思。自己的思路很简单,就是判断当前位置能不能放,能就放,不饿能就判断下一个位置。

代码:

class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        return helper(flowerbed, n);
    }
    private boolean helper(int[] flowerbed, int n){
        for(int i=0; i<flowerbed.length; i++){
            if(flowerbed[i] == 1 || i>0 && flowerbed[i-1] == 1 || i<flowerbed.length-1 && flowerbed[i+1] == 1){
                continue;
            }
            flowerbed[i] = 1;
            i++;
            n--;
        }
        return n<=0;
    }
    private boolean helper(int[] flowerbed, int n, int index){
        if(n == 0) return true;
        if(index >= flowerbed.length ) return false;
        if(flowerbed[index] == 1 ||
        index < flowerbed.length - 1 && flowerbed[index+1] == 1 || 
        index > 0 && flowerbed[index - 1] == 1){
            return helper(flowerbed, n, index + 1);
        }else{
            flowerbed[index] = 1;
            return helper(flowerbed, n-1, index+2);
        }
    }
}

笔记

标签:index,return,helper,int,flowerbed,问题,种花,&&,Leetcode605
来源: https://www.cnblogs.com/liuyongyu/p/14219825.html