8000 Sync LeetCode submission - Shortest Unsorted Continuous Subarray (java) · thatbeautifuldream/leetcode-sync@e5e6e89 · GitHub
[go: up one dir, main page]

Skip to content

Commit e5e6e89

Browse files
Sync LeetCode submission - Shortest Unsorted Continuous Subarray (java)
1 parent 4279537 commit e5e6e89

File tree

1 file changed

+29
-0
lines changed
  • practice/problems/shortest_unsorted_continuous_subarray

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

0 commit comments

Comments
 (0)
0