Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0e82dfa

Browse files
author
Sathish Babu
committed
✨ Added solution to 605
1 parent8b2f096 commit0e82dfa

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

‎src/0605.Can-Place-Flowers/README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,22 @@
55
66
##Description
77

8+
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.
9+
10+
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 n new flowers can be planted in it without violating the no-adjacent-flowers rule.
11+
812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: flowerbed = [1,0,0,0,1], n = 1
16+
Output: True
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: flowerbed = [1,0,0,0,1], n = 2
23+
Output: False
1324
```
1425

1526
##题意
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package Solution
22

3-
funcSolution(xbool)bool {
4-
returnx
3+
funcSolution(flowerbed []int,nint)bool {
4+
l,count:=len(flowerbed),0
5+
fori:=0;i<l;i++ {
6+
ifflowerbed[i]==0&& (i==0||flowerbed[i-1]==0)&& (i==l-1||flowerbed[i+1]==0) {
7+
flowerbed[i]=1
8+
count++
9+
}
10+
}
11+
returncount>=n
512
}

‎src/0605.Can-Place-Flowers/Solution_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@ import (
99
funcTestSolution(t*testing.T) {
1010
//测试用例
1111
cases:= []struct {
12-
namestring
13-
inputsbool
14-
expectbool
12+
namestring
13+
flowerbed []int
14+
nint
15+
expectbool
1516
}{
16-
{"TestCase",true,true},
17-
{"TestCase",true,true},
18-
{"TestCase",false,false},
17+
{"TestCase", []int{1,0,0,0,1},1,true},
18+
{"TestCase", []int{1,0,0,0,1},2,false},
19+
{"TestCase", []int{1,0,1,0},0,true},
20+
{"TestCase", []int{0},1,true},
21+
{"TestCase", []int{1,0,1,0,1,0,1},1,false},
1922
}
2023

2124
//开始测试
2225
fori,c:=rangecases {
2326
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
24-
got:=Solution(c.inputs)
27+
got:=Solution(c.flowerbed,c.n)
2528
if!reflect.DeepEqual(got,c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect,got,c.inputs)
29+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
30+
c.expect,got,c.flowerbed,c.n)
2831
}
2932
})
3033
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp