File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
practice/problems/shortest_unsorted_continuous_subarray Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public int findUnsortedSubarray (int [] nums ) {
3
+ int min = Integer .MAX_VALUE , max = Integer .MIN_VALUE ;
4
+ boolean flag = false ;
5
+ for (int i = 1 ; i < nums .length ; i ++) {
6
+ if (nums [i ] < nums [i - 1 ])
7
+ flag = true ;
8
+ if (flag )
9
+ min = Math .min (min , nums [i ]);
10
+ }
11
+ flag = false ;
12
+ for (int i = nums .length - 2 ; i >= 0 ; i --) {
13
+ if (nums [i ] > nums [i + 1 ])
14
+ flag = true ;
15
+ if (flag )
16
+ max = Math .max (max , nums [i ]);
17
+ }
18
+ int l , r ;
19
+ for (l = 0 ; l < nums .length ; l ++) {
20
+ if (min < nums [l ])
21
+ break ;
22
+ }
23
+ for (r = nums .length - 1 ; r >= 0 ; r --) {
24
+ if (max > nums [r ])
25
+ break ;
26
+ }
27
+ return r - l < 0 ? 0 : r - l + 1 ;
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments