File tree 2 files changed +29
-1
lines changed
algorithms/shortestUnsortedContinuousSubarray
2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 88
88
| 620| [ Not Boring Movies] ( https://leetcode.com/problems/not-boring-movies/ ) | [ Mysql] ( ./algorithms/notBoringMovies/Solution.sql ) | Easy|
89
89
| 617| [ Merge Two Binary Trees] ( https://leetcode.com/problems/merge-two-binary-trees/ ) | [ Java] ( ./algorithms/mergeTwoBinaryTrees/Solution.java ) | Easy|
90
90
| 595| [ Big Countries] ( https://leetcode.com/problems/big-countries/ ) | [ Mysql] ( ./algorithms/bigCountries/Solution.sql ) | Easy|
91
- | 581| [ Shortest Unsorted Continuous Subarray] ( https://leetcode.com/problems/shortest-unsorted-continuous-subarray/ ) | | Easy|
91
+ | 581| [ Shortest Unsorted Continuous Subarray] ( https://leetcode.com/problems/shortest-unsorted-continuous-subarray/ ) | [ Java_dream ] ( ./algorithms/shortestUnsortedContinuousSubarray/ShortestUnsortedContinuousSubarray.java ) | Easy|
92
92
| 575| [ Distrubute Candies] ( https://leetcode.com/problems/distribute-candies/ ) | [ java] ( ./algorithms/distributeCandies/Solution.java ) | Easy|
93
93
| 572| [ Subtree of Another Tree] ( https://leetcode.com/problems/subtree-of-another-tree/ ) | | Easy|
94
94
| 563| [ Binary Tree Tilt] ( https://leetcode.com/problems/binary-tree-tilt/ ) | [ java] ( ./algorithms/binaryTreeTilt/Solution.java ) | Easy|
Original file line number Diff line number Diff line change
1
+ import java .util .Deque ;
2
+ import java .util .LinkedList ;
3
+
4
+ /**
5
+ * @author Dream
6
+ */
7
+ public class ShortestUnsortedContinuousSubarray {
8
+ public int findUnsortedSubarray (int [] nums ) {
9
+ Deque <Integer > que = new LinkedList <>();
10
+ int left = nums .length ;
11
+ int right = 0 ;
12
+ boolean flag = false ;
13
+ for (int i = 0 ; i < nums .length ; i ++) {
14
+ if (!que .isEmpty () && nums [i ] < nums [que .getLast ()]) {
15
+ int temp = que .getLast ();
16
+ while (!que .isEmpty () && nums [i ] < nums [que .getLast ()]) {
17
+ left = Math .min (left , que .pollLast ());
18
+ }
19
+ que .addLast (temp );
20
+ right = i ;
21
+ flag = true ;
22
+ } else {
23
+ que .addLast (i );
24
+ }
25
+ }
26
+ return flag == true ? (right - left + 1 ) : 0 ;
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments