8000 solve problem Monotonic Array · P-ppc/leetcode@04fd222 · GitHub
[go: up one dir, main page]

Skip to content

Commit 04fd222

Browse files
committed
solve problem Monotonic Array
1 parent 28b6687 commit 04fd222

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ All solutions will be accepted!
300300
|103|[Binary Tree Zigzag Level Order Traversal](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/description/)|[java/py/js](./algorithms/BinaryTreeZigzagLevelOrderTraversal)|Medium|
301301
|134|[Gas Station](https://leetcode-cn.com/problems/gas-station/description/)|[java/py/js](./algorithms/GasStation)|Medium|
302302
|238|[Product Of Array Except Self](https://leetcode-cn.com/problems/product-of-array-except-self/description/)|[java/py/js](./algorithms/ProductOfArrayExceptSelf)|Medium|
303+
|896|[Monotonic Array](https://leetcode-cn.com/problems/monotonic-array/description/)|[java/py/js](./algorithms/MonotonicArray)|Medium|
303304

304305
# Database
305306
|#|Title|Solution|Difficulty|

algorithms/MonotonicArray/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Monotonic Array
2+
This problem is easy to solve
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public boolean isMonotonic(int[] A) {
3+
int v = 0;
4+
for (int i = 1; i < A.length; i++) {
5+
if (A[i - 1] > A[i])
6+
v |= 1;
7+
else if (A[i - 1] < A[i])
8+
v |= 2;
9+
}
10+
11+
return v != 3;
12+
}
13+
}

algorithms/MonotonicArray/solution.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} A
3+
* @return {boolean}
4+
*/
5+
var isMonotonic = function(A) {
6+
let v = 0
7+
for (let i = 1; i < A.length; i++) {
8+
if (A[i - 1] > A[i])
9+
v |= 1
10+
else if (A[i - 1] < A[i])
11+
v |= 2
12+
}
13+
14+
return v != 3
15+
};

algorithms/MonotonicArray/solution.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def isMonotonic(self, A):
3+
"""
4+
:type A: List[int]
5+
:rtype: bool
6+
"""
7+
v = 0
8+
for i in xrange(1, len(A)):
9+
if A[i - 1] > A[i]:
10+
v |= 1
11+
elif A[i - 1] < A[i]:
12+
v |= 2
13+
14+
return v != 3

0 commit comments

Comments
 (0)
0