Skip to content

Commit 8b1454e

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents ac49e87 + b8ef037 commit 8b1454e

7 files changed

+200
-0
lines changed

problems/0027.移除元素.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,30 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
246246
}
247247
```
248248

249+
PHP:
250+
```php
251+
class Solution {
252+
/**
253+
* @param Integer[] $nums
254+
* @param Integer $val
255+
* @return Integer
256+
*/
257+
function removeElement(&$nums, $val) {
258+
if (count($nums) == 0) {
259+
return 0;
260+
}
261+
// 快慢指针
262+
$slow = 0;
263+
for ($fast = 0; $fast < count($nums); $fast++) {
264+
if ($nums[$fast] != $val) {
265+
$nums[$slow] = $nums[$fast];
266+
$slow++;
267+
}
268+
}
269+
return $slow;
270+
}
271+
```
272+
249273
C:
250274
```c
251275
int removeElement(int* nums, int numsSize, int val){

problems/0059.螺旋矩阵II.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,48 @@ impl Solution {
426426
}
427427
```
428428

429+
PHP:
430+
```php
431+
class Solution {
432+
/**
433+
* @param Integer $n
434+
* @return Integer[][]
435+
*/
436+
function generateMatrix($n) {
437+
// 初始化数组
438+
$res = array_fill(0, $n, array_fill(0, $n, 0));
439+
$mid = $loop = floor($n / 2);
440+
$startX = $startY = 0;
441+
$offset = 1;
442+
$count = 1;
443+
while ($loop > 0) {
444+
$i = $startX;
445+
$j = $startY;
446+
for (; $j < $startY + $n - $offset; $j++) {
447+
$res[$i][$j] = $count++;
448+
}
449+
for (; $i < $startX + $n - $offset; $i++) {
450+
$res[$i][$j] = $count++;
451+
}
452+
for (; $j > $startY; $j--) {
453+
$res[$i][$j] = $count++;
454+
}
455+
for (; $i > $startX; $i--) {
456+
$res[$i][$j] = $count++;
457+
}
458+
$startX += 1;
459+
$startY += 1;
460+
$offset += 2;
461+
$loop--;
462+
}
463+
if ($n % 2 == 1) {
464+
$res[$mid][$mid] = $count;
465+
}
466+
return $res;
467+
}
468+
}
469+
```
470+
429471

430472
-----------------------
431473
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0209.长度最小的子数组.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,34 @@ impl Solution {
264264
}
265265
```
266266

267+
PHP:
268+
```php
269+
// 双指针 - 滑动窗口
270+
class Solution {
271+
/**
272+
* @param Integer $target
273+
* @param Integer[] $nums
274+
* @return Integer
275+
*/
276+
function minSubArrayLen($target, $nums) {
277+
if (count($nums) < 1) {
278+
return 0;
279+
}
280+
$sum = 0;
281+
$res = PHP_INT_MAX;
282+
$left = 0;
283+
for ($right = 0; $right < count($nums); $right++) {
284+
$sum += $nums[$right];
285+
while ($sum >= $target) {
286+
$res = min($res, $right - $left + 1);
287+
$sum -= $nums[$left];
288+
$left++;
289+
}
290+
}
291+
return $res == PHP_INT_MAX ? 0 : $res;
292+
}
293+
}
294+
```
267295

268296
-----------------------
269297
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0704.二分查找.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,38 @@ int search(int* nums, int numsSize, int target){
478478
}
479479
```
480480
481+
**PHP:**
482+
```php
483+
// 左闭右闭区间
484+
class Solution {
485+
/**
486+
* @param Integer[] $nums
487+
* @param Integer $target
488+
* @return Integer
489+
*/
490+
function search($nums, $target) {
491+
if (count($nums) == 0) {
492+
return -1;
493+
}
494+
$left = 0;
495+
$right = count($nums) - 1;
496+
while ($left <= $right) {
497+
$mid = floor(($left + $right) / 2);
498+
if ($nums[$mid] == $target) {
499+
return $mid;
500+
}
501+
if ($nums[$mid] > $target) {
502+
$right = $mid - 1;
503+
}
504+
else {
505+
$left = $mid + 1;
506+
}
507+
}
508+
return -1;
509+
}
510+
}
511+
```
512+
481513
-----------------------
482514
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
483515
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

problems/0714.买卖股票的最佳时机含手续费(动态规划).md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,25 @@ class Solution:
152152
```
153153

154154
Go:
155+
```Go
156+
func maxProfit(prices []int, fee int) int {
157+
n := len(prices)
158+
dp := make([][2]int, n)
159+
dp[0][0] = -prices[0]
160+
for i := 1; i < n; i++ {
161+
dp[i][1] = max(dp[i-1][1], dp[i-1][0]+prices[i]-fee)
162+
dp[i][0] = max(dp[i-1][0], dp[i-1][1]-prices[i])
163+
}
164+
return dp[n-1][1]
165+
}
166+
167+
func max(a, b int) int {
168+
if a > b {
169+
return a
170+
}
171+
return b
172+
}
173+
```
155174

156175
Javascript:
157176
```javascript

problems/0968.监控二叉树.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,34 @@ class Solution:
368368
return result
369369
```
370370
Go:
371+
```go
372+
const inf = math.MaxInt64 / 2
373+
374+
func minCameraCover(root *TreeNode) int {
375+
var dfs func(*TreeNode) (a, b, c int)
376+
dfs = func(node *TreeNode) (a, b, c int) {
377+
if node == nil {
378+
return inf, 0, 0
379+
}
380+
lefta, leftb, leftc := dfs(node.Left)
381+
righta, rightb, rightc := dfs(node.Right)
382+
a = leftc + rightc + 1
383+
b = min(a, min(lefta+rightb, righta+leftb))
384+
c = min(a, leftb+rightb)
385+
return
386+
}
387+
_, ans, _ := dfs(root)
388+
return ans
389+
}
371390

391+
func min(a, b int) int {
392+
if a <= b {
393+
return a
394+
}
395+
return b
396+
}
397+
398+
```
372399
Javascript:
373400
```Javascript
374401
var minCameraCover = function(root) {

problems/0977.有序数组的平方.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,34 @@ def sorted_squares(nums)
270270
end
271271
```
272272

273+
PHP:
274+
```php
275+
class Solution {
276+
/**
277+
* @param Integer[] $nums
278+
* @return Integer[]
279+
*/
280+
function sortedSquares($nums) {
281+
// 双指针法
282+
$res = [];
283+
for ($i = 0; $i < count($nums); $i++) {
284+
$res[$i] = 0;
285+
}
286+
$k = count($nums) - 1;
287+
for ($i = 0, $j = count($nums) - 1; $i <= $j; ) {
288+
if ($nums[$i] ** 2 < $nums[$j] ** 2) {
289+
$res[$k--] = $nums[$j] ** 2;
290+
$j--;
291+
}
292+
else {
293+
$res[$k--] = $nums[$i] ** 2;
294+
$i++;
295+
}
296+
}
297+
return $res;
298+
}
299+
}
300+
```
273301

274302

275303
-----------------------

0 commit comments

Comments
 (0)