Skip to content

Commit cbde385

Browse files
Sean PrashadSean Prashad
Sean Prashad
authored and
Sean Prashad
committed
Add 581_Shortest_Unsorted_Continuous_Subarray.java
1 parent 2325bce commit cbde385

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int findUnsortedSubarray(int[] nums) {
3+
if (nums == null || nums.length == 0) {
4+
return 0;
5+
}
6+
7+
int[] temp = nums.clone();
8+
Arrays.sort(temp);
9+
10+
int start = 0, end = nums.length - 1;
11+
12+
while (start < nums.length && nums[start] == temp[start]) {
13+
start++;
14+
}
15+
while (end > start && nums[end] == temp[end]) {
16+
end--;
17+
}
18+
19+
return end - start + 1;
20+
}
21+
}

0 commit comments

Comments
 (0)