8000 :sparkles: Added solution to 581 · reverse/awesome-golang-leetcode@61bde0b · GitHub
[go: up one dir, main page]

Skip to content

Commit 61bde0b

Browse files
author
Sathish Babu
committed
✨ Added solution to 581
1 parent 4264f0a commit 61bde0b

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

src/0581.Shortest-Unsorted-Continuous-Subarray/README.md

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

8+
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too.
9+
10+
You need to find the shortest such subarray and output its length.
11+
812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: [2, 6, 4, 8, 10, 9, 15]
16+
Output: 5
17+
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
1318
```
1419

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "math"
4+
5+
func Solution(nums []int) int {
6+
start, end, min, max := 0, -1, math.MaxInt32, math.MinInt32
7+
for i, j := 0, len(nums)-1; i < len(nums); i, j = i+1, j-1 {
8+
if nums[i] >= max {
9+
max = nums[i]
10+
} else {
11+
end = i
12+
}
13+
if nums[j] <= min {
14+
min = nums[j]
15+
} else {
16+
start = j
17+
}
18+
}
19+
return end - start + 1
520
}

src/0581.Shortest-Unsorted-Continuous-Subarray/Solution_test.go

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

2120
// 开始测试

0 commit comments

Comments
 (0)
0