File tree Expand file tree Collapse file tree 2 files changed +23
-19
lines changed
main/java/com/fishercoder/solutions/firstthousand
test/java/com/fishercoder/firstthousand Expand file tree Collapse file tree 2 files changed +23
-19
lines changed Original file line number Diff line number Diff line change 2
2
3
3
public class _896 {
4
4
public static class Solution1 {
5
- public boolean isMonotonic (int [] A ) {
5
+ public boolean isMonotonic (int [] nums ) {
6
6
int i = 0 ;
7
- for (; i < A .length - 1 ; i ++) {
8
- if (A [i ] <= A [i + 1 ]) {
9
- continue ;
10
- } else {
7
+ //check if it's increasing
8
+ for (; i < nums .length - 1 ; i ++) {
9
+ if (nums [i ] > nums [i + 1 ]) {
11
10
break ;
12
11
}
13
12
}
14
- if (i == A .length - 1 ) {
13
+ if (i == nums .length - 1 ) {
15
14
return true ;
16
15
}
17
16
i = 0 ;
18
- for (; i < A .length - 1 ; i ++) {
19
- if (A [i ] >= A [i + 1 ]) {
20
- continue ;
21
- } else {
17
+ //check if it's decreasing
18
+ for (; i < nums .length - 1 ; i ++) {
19
+ if (nums [i ] < nums [i + 1 ]) {
22
20
break ;
23
21
}
24
22
}
25
- return i == A .length - 1 ;
23
+ return i == nums .length - 1 ;
26
24
}
27
25
}
28
26
}
Original file line number Diff line number Diff line change 1
1
package com .fishercoder .firstthousand ;
2
2
3
3
import com .fishercoder .solutions .firstthousand ._896 ;
4
- import org .junit .BeforeClass ;
5
- import org .junit .Test ;
4
+ import org .junit .jupiter . api . BeforeEach ;
5
+ import org .junit .jupiter . api . Test ;
6
6
7
- import static org .junit .Assert .assertEquals ;
7
+ import static org .junit .jupiter . api . Assertions .assertEquals ;
8
8
9
9
public class _896Test {
10
10
private static _896 .Solution1 solution1 ;
11
- private static int [] A ;
11
+ private static int [] nums ;
12
12
13
- @ BeforeClass
14
- public static void setup () {
13
+ @ BeforeEach
14
+ public void setup () {
15
15
solution1 = new _896 .Solution1 ();
16
16
}
17
17
18
18
@ Test
19
19
public void test1 () {
20
- A = new int []{1 , 3 , 2 };
21
- assertEquals (false , solution1 .isMonotonic (A ));
20
+ nums = new int []{1 , 3 , 2 };
21
+ assertEquals (false , solution1 .isMonotonic (nums ));
22
+ }
23
+
24
+ @ Test
25
+ public void test2 () {
26
+ nums = new int []{6 , 5 , 4 , 4 };
27
+ assertEquals (true , solution1 .isMonotonic (nums ));
22
28
}
23
29
24
30
}
You can’t perform that action at this time.
0 commit comments