Skip to content

Commit 3aa1b9e

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0896
1 parent c55f7a6 commit 3aa1b9e

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

solution/0800-0899/0896.Monotonic Array/README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,49 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
遍历数组:
63+
64+
- 出现递减,将 `increase` 置为 `false`
65+
- 出现递增,将 `decrease` 置为 `false`
66+
- 既非递增也非递减,提前返回 `false`
67+
- 遍历结束,若出现递增或递减,返回 `true`
68+
6269
<!-- tabs:start -->
6370

6471
### **Python3**
6572

6673
<!-- 这里可写当前语言的特殊实现逻辑 -->
6774

6875
```python
69-
76+
class Solution:
77+
def isMonotonic(self, A: List[int]) -> bool:
78+
increase = decrease = True
79+
for i in range(1, len(A)):
80+
if not increase and not decrease:
81+
return False
82+
if A[i] < A[i - 1]:
83+
increase = False
84+
elif A[i] > A[i - 1]:
85+
decrease = False
86+
return increase or decrease
7087
```
7188

7289
### **Java**
7390

7491
<!-- 这里可写当前语言的特殊实现逻辑 -->
7592

7693
```java
77-
94+
class Solution {
95+
public boolean isMonotonic(int[] A) {
96+
boolean increase = true, decrease = true;
97+
for (int i = 1, n = A.length; i < n; ++i) {
98+
if (!increase && !decrease) return false;
99+
if (A[i] < A[i - 1]) decrease = false;
100+
else if (A[i] > A[i - 1]) increase = false;
101+
}
102+
return increase || decrease;
103+
}
104+
}
78105
```
79106

80107
### **...**

solution/0800-0899/0896.Monotonic Array/README_EN.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,33 @@
102102
### **Python3**
103103

104104
```python
105-
105+
class Solution:
106+
def isMonotonic(self, A: List[int]) -> bool:
107+
increase = decrease = True
108+
for i in range(1, len(A)):
109+
if not increase and not decrease:
110+
return False
111+
if A[i] < A[i - 1]:
112+
increase = False
113+
elif A[i] > A[i - 1]:
114+
decrease = False
115+
return increase or decrease
106116
```
107117

108118
### **Java**
109119

110120
```java
111-
121+
class Solution {
122+
public boolean isMonotonic(int[] A) {
123+
boolean increase = true, decrease = true;
124+
for (int i = 1, n = A.length; i < n; ++i) {
125+
if (!increase && !decrease) return false;
126+
if (A[i] < A[i - 1]) decrease = false;
127+
else if (A[i] > A[i - 1]) increase = false;
128+
}
129+
return increase || decrease;
130+
}
131+
}
112132
```
113133

114134
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public boolean isMonotonic(int[] A) {
3+
boolean increase = true, decrease = true;
4+
for (int i = 1, n = A.length; i < n; ++i) {
5+
if (!increase && !decrease) return false;
6+
if (A[i] < A[i - 1]) decrease = false;
7+
else if (A[i] > A[i - 1]) increase = false;
8+
}
9+
return increase || decrease;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def isMonotonic(self, A: List[int]) -> bool:
3+
increase = decrease = True
4+
for i in range(1, len(A)):
5+
if not increase and not decrease:
6+
return False
7+
if A[i] < A[i - 1]:
8+
increase = False
9+
elif A[i] > A[i - 1]:
10+
decrease = False
11+
return increase or decrease

0 commit comments

Comments
 (0)