Problem Solving with Algorithms

반응형

leetcode.com/problems/can-place-flowers/

 

Can Place Flowers - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제는 쉽다.

 

and 와 or을 잘 이해하고, 조건문을 얼마나 아름답게 쓰냐의 차이..

 

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;
    }
}
반응형
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band