Skip to content

Commit 2ffacda

Browse files
committed
Update Swift/581. Shortest Unsorted Continuous Subarray.swift
1 parent 7a20c94 commit 2ffacda

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// 581. Shortest Unsorted Continuous Subarray
2+
// 216 ms, 0%
3+
4+
func findUnsortedSubarray(_ nums: [Int]) -> Int {
5+
if nums.count < 2 { return 0 }
6+
let sn = nums.sorted()
7+
var p = -1, q = -1
8+
for (i, v) in zip(sn, nums).enumerated() {
9+
if v.0 != v.1 {
10+
p == -1 ? (p = i) : (q = i + 1)
11+
}
12+
}
13+
return q - p
14+
}

0 commit comments

Comments
 (0)