File tree Expand file tree Collapse file tree 15 files changed +490
-0
lines changed
0041.First Missing Positive Expand file tree Collapse file tree 15 files changed +490
-0
lines changed Original file line number Diff line number Diff line change @@ -222,6 +222,40 @@ void swap(int* a, int* b) {
222
222
}
223
223
```
224
224
225
+ ```php
226
+ class Solution {
227
+ /**
228
+ * @param integer[] $nums
229
+ * @return integer
230
+ */
231
+
232
+ function firstMissingPositive($nums) {
233
+ $n = count($nums);
234
+
235
+ for ($i = 0; $i < $n; $i++) {
236
+ if ($nums[$i] <= 0) {
237
+ $nums[$i] = $n + 1;
238
+ }
239
+ }
240
+
241
+ for ($i = 0; $i < $n; $i++) {
242
+ $num = abs($nums[$i]);
243
+ if ($num <= $n) {
244
+ $nums[$num - 1] = -abs($nums[$num - 1]);
245
+ }
246
+ }
247
+
248
+ for ($i = 0; $i < $n; $i++) {
249
+ if ($nums[$i] > 0) {
250
+ return $i + 1;
251
+ }
252
+ }
253
+
254
+ return $n + 1;
255
+ }
256
+ }
257
+ ```
258
+
225
259
<!-- tabs:end -->
226
260
227
261
<!-- end -->
Original file line number Diff line number Diff line change @@ -222,6 +222,40 @@ void swap(int* a, int* b) {
222
222
}
223
223
```
224
224
225
+ ```php
226
+ class Solution {
227
+ /**
228
+ * @param integer[] $nums
229
+ * @return integer
230
+ */
231
+
232
+ function firstMissingPositive($nums) {
233
+ $n = count($nums);
234
+
235
+ for ($i = 0; $i < $n; $i++) {
236
+ if ($nums[$i] <= 0) {
237
+ $nums[$i] = $n + 1;
238
+ }
239
+ }
240
+
241
+ for ($i = 0; $i < $n; $i++) {
242
+ $num = abs($nums[$i]);
243
+ if ($num <= $n) {
244
+ $nums[$num - 1] = -abs($nums[$num - 1]);
245
+ }
246
+ }
247
+
248
+ for ($i = 0; $i < $n; $i++) {
249
+ if ($nums[$i] > 0) {
250
+ return $i + 1;
251
+ }
252
+ }
253
+
254
+ return $n + 1;
255
+ }
256
+ }
257
+ ```
258
+
225
259
<!-- tabs:end -->
226
260
227
261
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ /**
3
+ * @param integer[] $nums
4
+ * @return integer
5
+ */
6
+
7
+ function firstMissingPositive ($nums ) {
8
+ $n = count ($nums );
9
+
10
+ for ($i = 0 ; $i < $n ; $i ++ ) {
11
+ if ($nums [$i ] <= 0 ) {
12
+ $nums [$i ] = $n + 1 ;
13
+ }
14
+ }
15
+
16
+ for ($i = 0 ; $i < $n ; $i ++ ) {
17
+ $num = abs ($nums [$i ]);
18
+ if ($num <= $n ) {
19
+ $nums [$num - 1 ] = - abs ($nums [$num - 1 ]);
20
+ }
21
+ }
22
+
23
+ for ($i = 0 ; $i < $n ; $i ++ ) {
24
+ if ($nums [$i ] > 0 ) {
25
+ return $i + 1 ;
26
+ }
27
+ }
28
+
29
+ return $n + 1 ;
30
+ }
31
+ }
Original file line number Diff line number Diff line change @@ -187,6 +187,48 @@ public class Solution {
187
187
}
188
188
```
189
189
190
+ ``` php
191
+ class Solution {
192
+ /**
193
+ * @param integer[] $height
194
+ * @return integer
195
+ */
196
+
197
+ function trap($height) {
198
+ $n = count($height);
199
+
200
+ if ($n == 0) {
201
+ return 0;
202
+ }
203
+
204
+ $left = 0;
205
+ $right = $n - 1;
206
+ $leftMax = 0;
207
+ $rightMax = 0;
208
+ $ans = 0;
209
+
210
+ while ($left < $right) {
211
+ if ($height[$left] < $height[$right]) {
212
+ if ($height[$left] > $leftMax) {
213
+ $leftMax = $height[$left];
214
+ } else {
215
+ $ans += $leftMax - $height[$left];
216
+ }
217
+ $left++;
218
+ } else {
219
+ if ($height[$right] > $rightMax) {
220
+ $rightMax = $height[$right];
221
+ } else {
222
+ $ans += $rightMax - $height[$right];
223
+ }
224
+ $right--;
225
+ }
226
+ }
227
+ return $ans;
228
+ }
229
+ }
230
+ ```
231
+
190
232
<!-- tabs: end -->
191
233
192
234
<!-- end -->
Original file line number Diff line number Diff line change @@ -181,6 +181,48 @@ public class Solution {
181
181
}
182
182
```
183
183
184
+ ``` php
185
+ class Solution {
186
+ /**
187
+ * @param integer[] $height
188
+ * @return integer
189
+ */
190
+
191
+ function trap($height) {
192
+ $n = count($height);
193
+
194
+ if ($n == 0) {
195
+ return 0;
196
+ }
197
+
198
+ $left = 0;
199
+ $right = $n - 1;
200
+ $leftMax = 0;
201
+ $rightMax = 0;
202
+ $ans = 0;
203
+
204
+ while ($left < $right) {
205
+ if ($height[$left] < $height[$right]) {
206
+ if ($height[$left] > $leftMax) {
207
+ $leftMax = $height[$left];
208
+ } else {
209
+ $ans += $leftMax - $height[$left];
210
+ }
211
+ $left++;
212
+ } else {
213
+ if ($height[$right] > $rightMax) {
214
+ $rightMax = $height[$right];
215
+ } else {
216
+ $ans += $rightMax - $height[$right];
217
+ }
218
+ $right--;
219
+ }
220
+ }
221
+ return $ans;
222
+ }
223
+ }
224
+ ```
225
+
184
226
<!-- tabs: end -->
185
227
186
228
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ /**
3
+ * @param integer[] $height
4
+ * @return integer
5
+ */
6
+
7
+ function trap ($height ) {
8
+ $n = count ($height );
9
+
10
+ if ($n == 0 ) {
11
+ return 0 ;
12
+ }
13
+
14
+ $left = 0 ;
15
+ $right = $n - 1 ;
16
+ $leftMax = 0 ;
17
+ $rightMax = 0 ;
18
+ $ans = 0 ;
19
+
20
+ while ($left < $right ) {
21
+ if ($height [$left ] < $height [$right ]) {
22
+ if ($height [$left ] > $leftMax ) {
23
+ $leftMax = $height [$left ];
24
+ } else {
25
+ $ans += $leftMax - $height [$left ];
26
+ }
27
+ $left ++ ;
28
+ } else {
29
+ if ($height [$right ] > $rightMax ) {
30
+ $rightMax = $height [$right ];
31
+ } else {
32
+ $ans += $rightMax - $height [$right ];
33
+ }
34
+ $right -- ;
35
+ }
36
+ }
37
+ return $ans ;
38
+ }
39
+ }
Original file line number Diff line number Diff line change @@ -263,6 +263,38 @@ public class Solution {
263
263
}
264
264
```
265
265
266
+ ``` php
267
+ class Solution {
268
+ /**
269
+ * @param string $num1
270
+ * @param string $num2
271
+ * @return string
272
+ */
273
+
274
+ function multiply($num1, $num2) {
275
+ $length1 = strlen($num1);
276
+ $length2 = strlen($num2);
277
+ $product = array_fill(0, $length1 + $length2, 0);
278
+
279
+ for ($i = $length1 - 1; $i >= 0; $i--) {
280
+ for ($j = $length2 - 1; $j >= 0; $j--) {
281
+ $digit1 = intval($num1[$i]);
282
+ $digit2 = intval($num2[$j]);
283
+
284
+ $temp = $digit1 * $digit2 + $product[$i + $j + 1];
285
+ $product[$i + $j + 1] = $temp % 10;
286
+
287
+ $carry = intval($temp / 10);
288
+ $product[$i + $j] += $carry;
289
+ }
290
+ }
291
+ $result = implode("", $product);
292
+ $result = ltrim($result, '0');
293
+ return $result === "" ? "0" : $result;
294
+ }
295
+ }
296
+ ```
297
+
266
298
<!-- tabs: end -->
267
299
268
300
<!-- end -->
Original file line number Diff line number Diff line change @@ -254,6 +254,38 @@ public class Solution {
254
254
}
255
255
```
256
256
257
+ ``` php
258
+ class Solution {
259
+ /**
260
+ * @param string $num1
261
+ * @param string $num2
262
+ * @return string
263
+ */
264
+
265
+ function multiply($num1, $num2) {
266
+ $length1 = strlen($num1);
267
+ $length2 = strlen($num2);
268
+ $product = array_fill(0, $length1 + $length2, 0);
269
+
270
+ for ($i = $length1 - 1; $i >= 0; $i--) {
271
+ for ($j = $length2 - 1; $j >= 0; $j--) {
272
+ $digit1 = intval($num1[$i]);
273
+ $digit2 = intval($num2[$j]);
274
+
275
+ $temp = $digit1 * $digit2 + $product[$i + $j + 1];
276
+ $product[$i + $j + 1] = $temp % 10;
277
+
278
+ $carry = intval($temp / 10);
279
+ $product[$i + $j] += $carry;
280
+ }
281
+ }
282
+ $result = implode("", $product);
283
+ $result = ltrim($result, '0');
284
+ return $result === "" ? "0" : $result;
285
+ }
286
+ }
287
+ ```
288
+
257
289
<!-- tabs: end -->
258
290
259
291
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ /**
3
+ * @param string $num1
4
+ * @param string $num2
5
+ * @return string
6
+ */
7
+
8
+ function multiply ($num1 , $num2 ) {
9
+ $length1 = strlen ($num1 );
10
+ $length2 = strlen ($num2 );
11
+ $product = array_fill (0 , $length1 + $length2 , 0 );
12
+
13
+ for ($i = $length1 - 1 ; $i >= 0 ; $i -- ) {
14
+ for ($j = $length2 - 1 ; $j >= 0 ; $j -- ) {
15
+ $digit1 = intval ($num1 [$i ]);
16
+ $digit2 = intval ($num2 [$j ]);
17
+
18
+ $temp = $digit1 * $digit2 + $product [$i + $j + 1 ];
19
+ $product [$i + $j + 1 ] = $temp % 10 ;
20
+
21
+ $carry = intval ($temp / 10 );
22
+ $product [$i + $j ] += $carry ;
23
+ }
24
+ }
25
+ $result = implode (" " , $product );
26
+ $result = ltrim ($result , ' 0' );
27
+ return $result === " " ? " 0" : $result ;
28
+ }
29
+ }
Original file line number Diff line number Diff line change @@ -400,6 +400,42 @@ function isMatch(s: string, p: string): boolean {
400
400
}
401
401
```
402
402
403
+ ``` php
404
+ class Solution {
405
+ /**
406
+ * @param string $s
407
+ * @param string $p
408
+ * @return boolean
409
+ */
410
+
411
+ function isMatch($s, $p) {
412
+ $lengthS = strlen($s);
413
+ $lengthP = strlen($p);
414
+ $dp = array();
415
+ for ($i = 0; $i <= $lengthS; $i++) {
416
+ $dp[$i] = array_fill(0, $lengthP + 1, false);
417
+ }
418
+ $dp[0][0] = true;
419
+
420
+ for ($i = 1; $i <= $lengthP; $i++) {
421
+ if ($p[$i - 1] == '*') {
422
+ $dp[0][$i] = $dp[0][$i - 1];
423
+ }
424
+ }
425
+ for ($i = 1; $i <= $lengthS; $i++) {
426
+ for ($j = 1; $j <= $lengthP; $j++) {
427
+ if ($p[$j - 1] == '?' || $s[$i - 1] == $p[$j - 1]) {
428
+ $dp[$i][$j] = $dp[$i - 1][$j - 1];
429
+ } else if ($p[$j - 1] == '*') {
430
+ $dp[$i][$j] = $dp[$i][$j - 1] || $dp[$i - 1][$j];
431
+ }
432
+ }
433
+ }
434
+ return $dp[$lengthS][$lengthP];
435
+ }
436
+ }
437
+ ```
438
+
403
439
<!-- tabs: end -->
404
440
405
441
<!-- end -->
Original file line number Diff line number Diff line change @@ -393,6 +393,42 @@ function isMatch(s: string, p: string): boolean {
393
393
}
394
394
```
395
395
396
+ ``` php
397
+ class Solution {
398
+ /**
399
+ * @param string $s
400
+ * @param string $p
401
+ * @return boolean
402
+ */
403
+
404
+ function isMatch($s, $p) {
405
+ $lengthS = strlen($s);
406
+ $lengthP = strlen($p);
407
+ $dp = array();
408
+ for ($i = 0; $i <= $lengthS; $i++) {
409
+ $dp[$i] = array_fill(0, $lengthP + 1, false);
410
+ }
411
+ $dp[0][0] = true;
412
+
413
+ for ($i = 1; $i <= $lengthP; $i++) {
414
+ if ($p[$i - 1] == '*') {
415
+ $dp[0][$i] = $dp[0][$i - 1];
416
+ }
417
+ }
418
+ for ($i = 1; $i <= $lengthS; $i++) {
419
+ for ($j = 1; $j <= $lengthP; $j++) {
420
+ if ($p[$j - 1] == '?' || $s[$i - 1] == $p[$j - 1]) {
421
+ $dp[$i][$j] = $dp[$i - 1][$j - 1];
422
+ } else if ($p[$j - 1] == '*') {
423
+ $dp[$i][$j] = $dp[$i][$j - 1] || $dp[$i - 1][$j];
424
+ }
425
+ }
426
+ }
427
+ return $dp[$lengthS][$lengthP];
428
+ }
429
+ }
430
+ ```
431
+
396
432
<!-- tabs: end -->
397
433
398
434
<!-- end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ /**
3
+ * @param string $s
4
+ * @param string $p
5
+ * @return boolean
6
+ */
7
+
8
+ function isMatch ($s , $p ) {
9
+ $lengthS = strlen ($s );
10
+ $lengthP = strlen ($p );
11
+ $dp = array ();
12
+ for ($i = 0 ; $i <= $lengthS ; $i ++ ) {
13
+ $dp [$i ] = array_fill (0 , $lengthP + 1 , false );
14
+ }
15
+ $dp [0 ][0 ] = true ;
16
+
17
+ for ($i = 1 ; $i <= $lengthP ; $i ++ ) {
18
+ if ($p [$i - 1 ] == ' *' ) {
19
+ $dp [0 ][$i ] = $dp [0 ][$i - 1 ];
20
+ }
21
+ }
22
+ for ($i = 1 ; $i <= $lengthS ; $i ++ ) {
23
+ for ($j = 1 ; $j <= $lengthP ; $j ++ ) {
24
+ if ($p [$j - 1 ] == ' ?' || $s [$i - 1 ] == $p [$j - 1 ]) {
25
+ $dp [$i ][$j ] = $dp [$i - 1 ][$j - 1 ];
26
+ } else if ($p [$j - 1 ] == ' *' ) {
27
+ $dp [$i ][$j ] = $dp [$i ][$j - 1 ] || $dp [$i - 1 ][$j ];
28
+ }
29
+ }
30
+ }
31
+ return $dp [$lengthS ][$lengthP ];
32
+ }
33
+ }
Original file line number Diff line number Diff line change @@ -192,6 +192,30 @@ int jump(int* nums, int numsSize) {
192
192
}
193
193
```
194
194
195
+ ```php
196
+ class Solution {
197
+ /**
198
+ * @param integer[] $nums
199
+ * @return integer
200
+ */
201
+
202
+ function jump($nums) {
203
+ $maxReach = 0;
204
+ $steps = 0;
205
+ $lastJump = 0;
206
+ for ($i = 0; $i <= count($nums) - 2; $i++) {
207
+ $maxReach = max($maxReach, $i + $nums[$i]);
208
+ if ($i == $lastJump) {
209
+ $lastJump = $maxReach;
210
+ $steps++;
211
+ }
212
+ }
213
+
214
+ return $steps;
215
+ }
216
+ }
217
+ ```
218
+
195
219
<!-- tabs:end -->
196
220
197
221
<!-- end -->
Original file line number Diff line number Diff line change @@ -187,6 +187,30 @@ int jump(int* nums, int numsSize) {
187
187
}
188
188
```
189
189
190
+ ```php
191
+ class Solution {
192
+ /**
193
+ * @param integer[] $nums
194
+ * @return integer
195
+ */
196
+
197
+ function jump($nums) {
198
+ $maxReach = 0;
199
+ $steps = 0;
200
+ $lastJump = 0;
201
+ for ($i = 0; $i <= count($nums) - 2; $i++) {
202
+ $maxReach = max($maxReach, $i + $nums[$i]);
203
+ if ($i == $lastJump) {
204
+ $lastJump = $maxReach;
205
+ $steps++;
206
+ }
207
+ }
208
+
209
+ return $steps;
210
+ }
211
+ }
212
+ ```
213
+
190
214
<!-- tabs:end -->
191
215
192
216
<!-- end -->
Original file line number Diff line number Diff line change
1
+ <?php
2
+ class Solution {
3
+ /**
4
+ * @param integer[] $nums
5
+ * @return integer
6
+ */
7
+
8
+ function jump ($ nums ) {
9
+ $ maxReach = 0 ;
10
+ $ steps = 0 ;
11
+ $ lastJump = 0 ;
12
+ for ($ i = 0 ; $ i <= count ($ nums ) - 2 ; $ i ++) {
13
+ $ maxReach = max ($ maxReach , $ i + $ nums [$ i ]);
14
+ if ($ i == $ lastJump ) {
15
+ $ lastJump = $ maxReach ;
16
+ $ steps ++;
17
+ }
18
+ }
19
+
20
+ return $ steps ;
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments