Skip to content

Commit efe5619

Browse files
Sean PrashadSean Prashad
authored andcommitted
Update 581_Shortest_Unsorted_Continuous_Subarray.java
1 parent 57af2b4 commit efe5619

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Arrays/581_Shortest_Unsorted_Continuous_Subarray.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ public int findUnsortedSubarray(int[] nums) {
55
}
66

77
int[] temp = nums.clone();
8-
Arrays.sort(temp);
8+
int left = 0, right = temp.length - 1;
99

10-
int start = 0, end = nums.length - 1;
10+
Arrays.sort(temp);
1111

12-
while (start < nums.length && nums[start] == temp[start]) {
13-
start++;
12+
while (left <= right && nums[left] == temp[left]) {
13+
++left;
1414
}
15-
while (end > start && nums[end] == temp[end]) {
16-
end--;
15+
while (left <= right && nums[right] == temp[right]) {
16+
--right;
1717
}
1818

19-
return end - start + 1;
19+
return right - left + 1;
2020
}
2121
}

0 commit comments

Comments
 (0)