73
73
74
74
<!-- solution:start -->
75
75
76
- ### 方法一
76
+ ### 方法一:遍历
77
+
78
+ 我们可以遍历数组,找到第一个不满足 $\textit{nums}[ i] < \textit{nums}[ i+1] $ 的位置 $i$,然后检查删除 $i$ 或者 $i+1$ 后的数组是否严格递增,如果是则返回 $\textit{true}$,否则返回 $\textit{false}$。
79
+
80
+ 时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
77
81
78
82
<!-- tabs:start -->
79
83
@@ -82,43 +86,44 @@ tags:
82
86
``` python
83
87
class Solution :
84
88
def canBeIncreasing (self , nums : List[int ]) -> bool :
85
- def check (nums , i ) :
86
- prev = - inf
87
- for j, num in enumerate (nums):
88
- if i == j :
89
+ def check (k : int ) -> bool :
90
+ pre = - inf
91
+ for i, x in enumerate (nums):
92
+ if i == k :
89
93
continue
90
- if prev >= nums[j] :
94
+ if pre >= x :
91
95
return False
92
- prev = nums[j]
96
+ pre = x
93
97
return True
94
98
95
- i, n = 1 , len (nums)
96
- while i < n and nums[i - 1 ] < nums[i]:
99
+ i = 0
100
+ while i + 1 < len (nums) and nums[i] < nums[i + 1 ]:
97
101
i += 1
98
- return check(nums, i - 1 ) or check(nums, i )
102
+ return check(i ) or check(i + 1 )
99
103
```
100
104
101
105
#### Java
102
106
103
107
``` java
104
108
class Solution {
105
109
public boolean canBeIncreasing (int [] nums ) {
106
- int i = 1 , n = nums. length;
107
- for (; i < n && nums[i - 1 ] < nums[i]; ++ i)
108
- ;
109
- return check(nums, i - 1 ) || check(nums, i);
110
+ int i = 0 ;
111
+ while (i + 1 < nums. length && nums[i] < nums[i + 1 ]) {
112
+ ++ i;
113
+ }
114
+ return check(nums, i) || check(nums, i + 1 );
110
115
}
111
116
112
- private boolean check (int [] nums , int i ) {
113
- int prev = Integer . MIN_VALUE ;
114
- for (int j = 0 ; j < nums. length; ++ j ) {
115
- if (i == j ) {
117
+ private boolean check (int [] nums , int k ) {
118
+ int pre = 0 ;
119
+ for (int i = 0 ; i < nums. length; ++ i ) {
120
+ if (i == k ) {
116
121
continue ;
117
122
}
118
- if (prev >= nums[j ]) {
123
+ if (pre >= nums[i ]) {
119
124
return false ;
120
125
}
121
- prev = nums[j ];
126
+ pre = nums[i ];
122
127
}
123
128
return true ;
124
129
}
@@ -131,20 +136,25 @@ class Solution {
131
136
class Solution {
132
137
public:
133
138
bool canBeIncreasing(vector<int >& nums) {
134
- int i = 1, n = nums.size();
135
- for (; i < n && nums[ i - 1] < nums[ i] ; ++i)
136
- ;
137
- return check(nums, i - 1) || check(nums, i);
138
- }
139
-
140
- bool check(vector<int>& nums, int i) {
141
- int prev = 0;
142
- for (int j = 0; j < nums.size(); ++j) {
143
- if (i == j) continue;
144
- if (prev >= nums[j]) return false;
145
- prev = nums[j];
139
+ int n = nums.size();
140
+ auto check = [ &] (int k) -> bool {
141
+ int pre = 0;
142
+ for (int i = 0; i < n; ++i) {
143
+ if (i == k) {
144
+ continue;
145
+ }
146
+ if (pre >= nums[ i] ) {
147
+ return false;
148
+ }
149
+ pre = nums[ i] ;
150
+ }
151
+ return true;
152
+ };
153
+ int i = 0;
154
+ while (i + 1 < n && nums[ i] < nums[ i + 1] ) {
155
+ ++i;
146
156
}
147
- return true ;
157
+ return check(i) || check(i + 1) ;
148
158
}
149
159
};
150
160
```
@@ -153,50 +163,50 @@ public:
153
163
154
164
```go
155
165
func canBeIncreasing(nums []int) bool {
156
- i , n := 1 , len (nums)
157
- for ; i < n && nums[i-1 ] < nums[i]; i++ {
158
-
159
- }
160
- return check (nums, i-1 ) || check (nums, i)
161
- }
162
-
163
- func check (nums []int , i int ) bool {
164
- prev := 0
165
- for j := 0 ; j < len (nums); j++ {
166
- if i == j {
167
- continue
168
- }
169
- if prev >= nums[j] {
170
- return false
166
+ check := func(k int) bool {
167
+ pre := 0
168
+ for i, x := range nums {
169
+ if i == k {
170
+ continue
171
+ }
172
+ if pre >= x {
173
+ return false
174
+ }
175
+ pre = x
171
176
}
172
- prev = nums[j]
177
+ return true
173
178
}
174
- return true
179
+ i := 0
180
+ for i+1 < len(nums) && nums[i] < nums[i+1] {
181
+ i++
182
+ }
183
+ return check(i) || check(i+1)
175
184
}
176
185
```
177
186
178
187
#### TypeScript
179
188
180
189
``` ts
181
190
function canBeIncreasing(nums : number []): boolean {
182
- const check = (p : number ) => {
183
- let prev = undefined ;
184
- for (let j = 0 ; j < nums .length ; j ++ ) {
185
- if (p != j ) {
186
- if (prev !== undefined && prev >= nums [j ]) {
187
- return false ;
188
- }
189
- prev = nums [j ];
191
+ const n = nums .length ;
192
+ const check = (k : number ): boolean => {
193
+ let pre = 0 ;
194
+ for (let i = 0 ; i < n ; ++ i ) {
195
+ if (i === k ) {
196
+ continue ;
190
197
}
198
+ if (pre >= nums [i ]) {
199
+ return false ;
200
+ }
201
+ pre = nums [i ];
191
202
}
192
203
return true ;
193
204
};
194
- for (let i = 0 ; i < nums .length ; i ++ ) {
195
- if (nums [i - 1 ] >= nums [i ]) {
196
- return check (i - 1 ) || check (i );
197
- }
205
+ let i = 0 ;
206
+ while (i + 1 < n && nums [i ] < nums [i + 1 ]) {
207
+ ++ i ;
198
208
}
199
- return true ;
209
+ return check ( i ) || check ( i + 1 ) ;
200
210
}
201
211
```
202
212
@@ -205,26 +215,53 @@ function canBeIncreasing(nums: number[]): boolean {
205
215
``` rust
206
216
impl Solution {
207
217
pub fn can_be_increasing (nums : Vec <i32 >) -> bool {
208
- let check = | p : usize | -> bool {
209
- let mut prev = None ;
210
- for j in 0 .. nums . len () {
211
- if p != j {
212
- if let Some (value ) = prev {
213
- if value >= nums [j ] {
214
- return false ;
215
- }
216
- }
217
- prev = Some (nums [j ]);
218
+ let check = | k : usize | -> bool {
219
+ let mut pre = 0 ;
220
+ for (i , & x ) in nums . iter (). enumerate () {
221
+ if i == k {
222
+ continue ;
218
223
}
224
+ if pre >= x {
225
+ return false ;
226
+ }
227
+ pre = x ;
219
228
}
220
229
true
221
230
};
222
- for i in 1 .. nums . len () {
223
- if nums [i - 1 ] >= nums [i ] {
224
- return check (i - 1 ) || check (i );
231
+
232
+ let mut i = 0 ;
233
+ while i + 1 < nums . len () && nums [i ] < nums [i + 1 ] {
234
+ i += 1 ;
235
+ }
236
+ check (i ) || check (i + 1 )
237
+ }
238
+ }
239
+ ```
240
+
241
+ #### C#
242
+
243
+ ``` cs
244
+ public class Solution {
245
+ public bool CanBeIncreasing (int [] nums ) {
246
+ int n = nums .Length ;
247
+ bool check (int k ) {
248
+ int pre = 0 ;
249
+ for (int i = 0 ; i < n ; ++ i ) {
250
+ if (i == k ) {
251
+ continue ;
252
+ }
253
+ if (pre >= nums [i ]) {
254
+ return false ;
255
+ }
256
+ pre = nums [i ];
225
257
}
258
+ return true ;
259
+ }
260
+ int i = 0 ;
261
+ while (i + 1 < n && nums [i ] < nums [i + 1 ]) {
262
+ ++ i ;
226
263
}
227
- true
264
+ return check ( i ) || check ( i + 1 );
228
265
}
229
266
}
230
267
```
0 commit comments