8000 :sparkles: Added solution to 665 · huxulm/awesome-golang-leetcode@40ca759 · GitHub
[go: up one dir, main page]

Skip to content

Commit 40ca759

Browse files
author
Sathish Babu
committed
✨ Added solution to 665
1 parent 43322d4 commit 40ca759

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

src/0665.Non-decreasing-Array/README.md

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

8+
Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.
9+
10+
We define an array is non-decreasing if nums[i] <= nums[i + 1] holds for every i (0-based) such that (0 <= i <= n - 2).
11+
812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: nums = [4,2,3]
16+
Output: true
17+
Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Input: nums = [4,2,1]
24+
Output: false
25+
Explanation: You can't get a non-decreasing array by modify at most one element.
26+
1327
```
1428

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) bool {
4+
c := 0
5+
for i := 0; i < len(nums)-1; i++ {
6+
if nums[i] > nums[i+1] {
7+
if i > 0 {
8+
if nums[i-1] <= nums[i+1] {
9+
nums[i] = nums[i-1]
10+
} else {
11+
nums[i+1] = nums[i]
12+
}
13+
}
14+
c++
15+
}
16+
}
17+
return c <= 1
518
}

src/0665.Non-decreasing-Array/Solution_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs []int
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", []int{4, 2, 3}, true},
17+
{"TestCase", []int{4, 2, 1}, false},
18+
{"TestCase", []int{1, 2, 5, 4}, true},
1919
}
2020

2121
// 开始测试

0 commit comments

Comments
 (0)
0