Skip to content

Commit 2820cf6

Browse files
author
Dream
committed
[update] 581
1 parent 7b8fc54 commit 2820cf6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
|620|[Not Boring Movies](https://leetcode.com/problems/not-boring-movies/)| [Mysql](./algorithms/notBoringMovies/Solution.sql) |Easy|
8989
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)| [Java](./algorithms/mergeTwoBinaryTrees/Solution.java) |Easy|
9090
|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|
9292
|575|[Distrubute Candies](https://leetcode.com/problems/distribute-candies/)| [java](./algorithms/distributeCandies/Solution.java) |Easy|
9393
|572|[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/) | |Easy|
9494
|563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [java](./algorithms/binaryTreeTilt/Solution.java) |Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

0 commit comments

Comments
 (0)