Skip to content

Commit 9472098

Browse files
BarklimBarklim
authored andcommitted
Create 0896.js
1 parent fc896ed commit 9472098

File tree

1 file changed

+38
-0
lines changed
  • example/ProgrammingSkills/0.Basic

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
};

0 commit comments

Comments
 (0)