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

Commit61bde0b

Browse files
author
Sathish Babu
committed
✨ Added solution to 581
1 parent4264f0a commit61bde0b

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-
funcSolution(xbool)bool {
4-
returnx
3+
import"math"
4+
5+
funcSolution(nums []int)int {
6+
start,end,min,max:=0,-1,math.MaxInt32,math.MinInt32
7+
fori,j:=0,len(nums)-1;i<len(nums);i,j=i+1,j-1 {
8+
ifnums[i]>=max {
9+
max=nums[i]
10+
}else {
11+
end=i
12+
}
13+
ifnums[j]<=min {
14+
min=nums[j]
15+
}else {
16+
start=j
17+
}
18+
}
19+
returnend-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
namestring
13-
inputsbool
14-
expectbool
13+
inputs[]int
14+
expectint
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)

[8]ページ先頭

©2009-2025 Movatter.jp