File tree Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public boolean isMonotonic (int [] nums ) {
3
- if ( nums . length < 2 ) {
4
- return true ;
5
- }
6
- Boolean increasing = null ;
7
- for ( int i = 1 ; i < nums . length ; i ++) {
8
- if (nums [i ] == nums [i - 1 ] && increasing == null ) {
9
- continue ;
3
+ int idx = 0 ;
4
+ int sign = 0 ;
5
+ while ( idx < nums . length - 1 && sign == 0 ) {
6
+ if ( nums [ idx ] < nums [ idx + 1 ]) {
7
+ sign = 1 ;
8
+ } else if (nums [idx ] > nums [idx + 1 ]) {
9
+ sign = - 1 ;
10
10
}
11
- increasing = increasing == null ? nums [i ] > nums [i - 1 ] : increasing ;
12
- if ((nums [i ] > nums [i - 1 ] && !increasing ) || (nums [i ] < nums [i - 1 ] && increasing )) {
11
+ idx ++;
12
+ }
13
+ while (idx < nums .length - 1 ) {
14
+ if ((sign == 1 && nums [idx ] > nums [idx + 1 ]) || (sign == -1 && nums [idx ] < nums [idx + 1 ])) {
13
15
return false ;
14
16
}
17
+ idx ++;
15
18
}
16
19
return true ;
17
20
}
You can’t perform that action at this time.
0 commit comments