Skip to content

Commit 8ca5525

Browse files
authored
Update Check if Array Is Sorted and Rotated.java
1 parent 7fa0347 commit 8ca5525

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
class Solution {
22
public boolean check(int[] nums) {
3-
boolean rotationFound = false;
3+
boolean rotated = false;
44
for (int i = 0; i < nums.length - 1; i++) {
55
if (nums[i] > nums[i + 1]) {
6-
if (rotationFound) {
6+
if (rotated) {
77
return false;
88
}
9-
rotationFound = true;
9+
rotated = true;
1010
}
1111
}
12-
if (rotationFound && nums[nums.length - 1] > nums[0]) {
13-
return false;
14-
}
15-
return true;
12+
return !rotated || nums[nums.length - 1] <= nums[0];
1613
}
1714
}

0 commit comments

Comments
 (0)