Skip to content

Commit 7e4f047

Browse files
committed
Add Solution.java for 0581.Shortest Unsorted Continuous Subarray
1 parent db46f93 commit 7e4f047

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int findUnsortedSubarray(int[] nums) {
3+
int[] numArrs = nums.clone();
4+
Arrays.sort(numArrs);
5+
6+
int start = numArrs.length, end = 0;
7+
for (int i = 0; i < numArrs.length; i++) {
8+
if (numArrs[i] != nums[i]) {
9+
start = Math.min(start, i);
10+
end = Math.max(end, i);
11+
}
12+
}
13+
return (end - start >= 0 ? end - start + 1 : 0);
14+
}
15+
}

0 commit comments

Comments
 (0)