mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0209.长度最小的子数组 PHP版本
This commit is contained in:
@ -264,6 +264,34 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
// 双指针 - 滑动窗口
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer $target
|
||||
* @param Integer[] $nums
|
||||
* @return Integer
|
||||
*/
|
||||
function minSubArrayLen($target, $nums) {
|
||||
if (count($nums) < 1) {
|
||||
return 0;
|
||||
}
|
||||
$sum = 0;
|
||||
$res = PHP_INT_MAX;
|
||||
$left = 0;
|
||||
for ($right = 0; $right < count($nums); $right++) {
|
||||
$sum += $nums[$right];
|
||||
while ($sum >= $target) {
|
||||
$res = min($res, $right - $left + 1);
|
||||
$sum -= $nums[$left];
|
||||
$left++;
|
||||
}
|
||||
}
|
||||
return $res == PHP_INT_MAX ? 0 : $res;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
|
Reference in New Issue
Block a user