Skip to content

Commit 9beae7c

Browse files
committed
Add solution 695 [Java]
1 parent 1c8b53e commit 9beae7c

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
4242
| 237 | [Delete Node in a Linked List](https://github.com/doocs/leetcode/tree/master/solution/237.Delete%20Node%20in%20a%20Linked%20List) | `Linked List` |
4343
| 344 | [Reverse String](https://github.com/doocs/leetcode/tree/master/solution/344.Reverse%20String) | `Two Pointers`, `String` |
4444
| 581 | [Shortest Unsorted Continuous Subarray](https://github.com/doocs/leetcode/tree/master/solution/581.Shortest%20Unsorted%20Continuous%20Subarray) | `Array` |
45+
| 605 | [Can Place Flowers](https://github.com/doocs/leetcode/tree/master/solution/605.Can%20Place%20Flowers) | `Array` |
4546
| 695 | [Max Area of Island](https://github.com/doocs/leetcode/tree/master/solution/695.Max%20Area%20of%20Island) | `Array`, `Depth-first Search` |
4647
| 703 | [Kth Largest Element in a Stream](https://github.com/doocs/leetcode/tree/master/solution/703.Kth%20Largest%20Element%20in%20a%20Stream) | `Heap` |
4748
| 707 | [Design Linked List](https://github.com/doocs/leetcode/tree/master/solution/707.Design%20Linked%20List) | `Linked List`, `Design` |
@@ -101,6 +102,8 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http
101102
- Fork this repository to your own GitHub account and then [clone](https://help.github.com/articles/cloning-a-repository/) it to your local device.
102103
- Submit a pull request with your changes!
103104

105+
![how-to-contribute](http://p9ucdlghd.bkt.clouddn.com/how-to-contribute-yanglbme.png)
106+
104107
## Contributors
105108

106109
" If you want to go fast, go alone. If you want to go far, go together. And that's the spirit of [teamwork](https://github.com/doocs/leetcode/graphs/contributors) ".
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## 种花问题
2+
### 题目描述
3+
4+
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。
5+
6+
给定一个花坛(表示为一个数组包含 0 和 1,其中 0 表示没种植花,1 表示种植了花),和一个数 `n` 。能否在不打破种植规则的情况下种入 `n` 朵花?能则返回 True,不能则返回 False。
7+
8+
**示例 1:**
9+
```
10+
输入: flowerbed = [1,0,0,0,1], n = 1
11+
输出: True
12+
```
13+
14+
**示例 2:**
15+
```
16+
输入: flowerbed = [1,0,0,0,1], n = 2
17+
输出: False
18+
```
19+
20+
**注意:**
21+
22+
- 数组内已种好的花不会违反种植规则。
23+
- 输入的数组长度范围为 [1, 20000]
24+
- n 是非负整数,且不会超过输入数组的大小。
25+
26+
### 解法
27+
遍历数组,若当前位置的元素为 0 并且左右元素也为 0,则该位置可以种花,计数器 +1。
28+
29+
注意,需要特殊处理数组首尾元素。
30+
31+
```java
32+
class Solution {
33+
public boolean canPlaceFlowers(int[] flowerbed, int n) {
34+
int len = flowerbed.length;
35+
int cnt = 0;
36+
for (int i = 0; i < len; ++i) {
37+
if (flowerbed[i] == 0
38+
&& (i == 0 || flowerbed[i - 1] == 0)
39+
&& (i == len - 1 || flowerbed[i + 1] == 0)) {
40+
++cnt;
41+
flowerbed[i] = 1;
42+
}
43+
}
44+
return cnt >= n;
45+
}
46+
}
47+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public boolean canPlaceFlowers(int[] flowerbed, int n) {
3+
int len = flowerbed.length;
4+
int cnt = 0;
5+
for (int i = 0; i < len; ++i) {
6+
if (flowerbed[i] == 0
7+
&& (i == 0 || flowerbed[i - 1] == 0)
8+
&& (i == len - 1 || flowerbed[i + 1] == 0)) {
9+
++cnt;
10+
flowerbed[i] = 1;
11+
}
12+
}
13+
return cnt >= n;
14+
}
15+
}

0 commit comments

Comments
 (0)