File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
example/ProgrammingSkills/0.Basic Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {boolean }
4
+ */
5
+ var isMonotonic = function ( nums ) {
6
+
7
+ } ;
8
+
9
+ const example1 = isMonotonic ( [ 1 , 2 , 2 , 3 ] ) ; // true
10
+ const example2 = isMonotonic ( [ 6 , 5 , 4 , 4 ] ) ; // true
11
+ const example3 = isMonotonic ( [ 1 , 3 , 2 ] ) ; // false
12
+
13
+ console . log ( example1 ) ;
14
+ console . log ( example2 ) ;
15
+ console . log ( example3 ) ;
16
+
17
+ var isMonotonic = function ( nums ) {
18
+ let n = nums . length
19
+ if ( n === 1 ) return true
20
+
21
+ let isInc = true
22
+ let isDec = true
23
+
24
+ for ( let i = 1 ; i < n ; i ++ ) {
25
+ if ( ! isInc && ! isDec ) {
26
+ return false
27
+ }
28
+
29
+ if ( nums [ i - 1 ] > nums [ i ] ) {
30
+ isInc = false
31
+ }
32
+
33
+ if ( nums [ i - 1 ] < nums [ i ] ) {
34
+ isDec = false
35
+ }
36
+ }
37
+ return isInc || isDec
38
+ } ;
You can’t perform that action at this time.
0 commit comments