mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -246,6 +246,30 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer[] $nums
|
||||
* @param Integer $val
|
||||
* @return Integer
|
||||
*/
|
||||
function removeElement(&$nums, $val) {
|
||||
if (count($nums) == 0) {
|
||||
return 0;
|
||||
}
|
||||
// 快慢指针
|
||||
$slow = 0;
|
||||
for ($fast = 0; $fast < count($nums); $fast++) {
|
||||
if ($nums[$fast] != $val) {
|
||||
$nums[$slow] = $nums[$fast];
|
||||
$slow++;
|
||||
}
|
||||
}
|
||||
return $slow;
|
||||
}
|
||||
```
|
||||
|
||||
C:
|
||||
```c
|
||||
int removeElement(int* nums, int numsSize, int val){
|
||||
|
@ -426,6 +426,48 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer $n
|
||||
* @return Integer[][]
|
||||
*/
|
||||
function generateMatrix($n) {
|
||||
// 初始化数组
|
||||
$res = array_fill(0, $n, array_fill(0, $n, 0));
|
||||
$mid = $loop = floor($n / 2);
|
||||
$startX = $startY = 0;
|
||||
$offset = 1;
|
||||
$count = 1;
|
||||
while ($loop > 0) {
|
||||
$i = $startX;
|
||||
$j = $startY;
|
||||
for (; $j < $startY + $n - $offset; $j++) {
|
||||
$res[$i][$j] = $count++;
|
||||
}
|
||||
for (; $i < $startX + $n - $offset; $i++) {
|
||||
$res[$i][$j] = $count++;
|
||||
}
|
||||
for (; $j > $startY; $j--) {
|
||||
$res[$i][$j] = $count++;
|
||||
}
|
||||
for (; $i > $startX; $i--) {
|
||||
$res[$i][$j] = $count++;
|
||||
}
|
||||
$startX += 1;
|
||||
$startY += 1;
|
||||
$offset += 2;
|
||||
$loop--;
|
||||
}
|
||||
if ($n % 2 == 1) {
|
||||
$res[$mid][$mid] = $count;
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
|
@ -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)
|
||||
|
@ -478,6 +478,38 @@ int search(int* nums, int numsSize, int target){
|
||||
}
|
||||
```
|
||||
|
||||
**PHP:**
|
||||
```php
|
||||
// 左闭右闭区间
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer[] $nums
|
||||
* @param Integer $target
|
||||
* @return Integer
|
||||
*/
|
||||
function search($nums, $target) {
|
||||
if (count($nums) == 0) {
|
||||
return -1;
|
||||
}
|
||||
$left = 0;
|
||||
$right = count($nums) - 1;
|
||||
while ($left <= $right) {
|
||||
$mid = floor(($left + $right) / 2);
|
||||
if ($nums[$mid] == $target) {
|
||||
return $mid;
|
||||
}
|
||||
if ($nums[$mid] > $target) {
|
||||
$right = $mid - 1;
|
||||
}
|
||||
else {
|
||||
$left = $mid + 1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
|
@ -152,6 +152,25 @@ class Solution:
|
||||
```
|
||||
|
||||
Go:
|
||||
```Go
|
||||
func maxProfit(prices []int, fee int) int {
|
||||
n := len(prices)
|
||||
dp := make([][2]int, n)
|
||||
dp[0][0] = -prices[0]
|
||||
for i := 1; i < n; i++ {
|
||||
dp[i][1] = max(dp[i-1][1], dp[i-1][0]+prices[i]-fee)
|
||||
dp[i][0] = max(dp[i-1][0], dp[i-1][1]-prices[i])
|
||||
}
|
||||
return dp[n-1][1]
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
```javascript
|
||||
|
@ -368,7 +368,34 @@ class Solution:
|
||||
return result
|
||||
```
|
||||
Go:
|
||||
```go
|
||||
const inf = math.MaxInt64 / 2
|
||||
|
||||
func minCameraCover(root *TreeNode) int {
|
||||
var dfs func(*TreeNode) (a, b, c int)
|
||||
dfs = func(node *TreeNode) (a, b, c int) {
|
||||
if node == nil {
|
||||
return inf, 0, 0
|
||||
}
|
||||
lefta, leftb, leftc := dfs(node.Left)
|
||||
righta, rightb, rightc := dfs(node.Right)
|
||||
a = leftc + rightc + 1
|
||||
b = min(a, min(lefta+rightb, righta+leftb))
|
||||
c = min(a, leftb+rightb)
|
||||
return
|
||||
}
|
||||
_, ans, _ := dfs(root)
|
||||
return ans
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a <= b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
```
|
||||
Javascript:
|
||||
```Javascript
|
||||
var minCameraCover = function(root) {
|
||||
|
@ -270,6 +270,34 @@ def sorted_squares(nums)
|
||||
end
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer[] $nums
|
||||
* @return Integer[]
|
||||
*/
|
||||
function sortedSquares($nums) {
|
||||
// 双指针法
|
||||
$res = [];
|
||||
for ($i = 0; $i < count($nums); $i++) {
|
||||
$res[$i] = 0;
|
||||
}
|
||||
$k = count($nums) - 1;
|
||||
for ($i = 0, $j = count($nums) - 1; $i <= $j; ) {
|
||||
if ($nums[$i] ** 2 < $nums[$j] ** 2) {
|
||||
$res[$k--] = $nums[$j] ** 2;
|
||||
$j--;
|
||||
}
|
||||
else {
|
||||
$res[$k--] = $nums[$i] ** 2;
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user