Merge branch 'youngyangyang04:master' into master

This commit is contained in:
KailokFung
2021-07-03 16:12:49 +08:00
committed by GitHub
5 changed files with 125 additions and 0 deletions

View File

@ -187,7 +187,23 @@ var twoSum = function (nums, target) {
};
```
php
```php
function twoSum(array $nums, int $target): array
{
for ($i = 0; $i < count($nums);$i++) {
// 计算剩下的数
$residue = $target - $nums[$i];
// 匹配的index有则返回index 无则返回false
$match_index = array_search($residue, $nums);
if ($match_index !== false && $match_index != $i) {
return array($i, $match_index);
}
}
return [];
}
```
-----------------------

View File

@ -354,6 +354,47 @@ def is_valid(strs)
end
```
php:
```php
function threeSum(array $nums): array
{
$result = [];
$length = count($nums);
if ($length < 3) {
return [];
}
sort($nums);
for ($i = 0; $i < $length; $i++) {
// 如果大于0结束
if ($nums[$i] > 0) break;
// 去重
if ($i > 0 && $nums[$i] == $nums[$i - 1]) continue;
$left = $i + 1;
$right = $length - 1;
// 比较
while ($left < $right) {
$sum = $nums[$i] + $nums[$left] + $nums[$right];
if ($sum < 0) {
$left++;
} elseif ($sum > 0) {
$right--;
} else {
array_push($result, [$nums[$i], $nums[$left], $nums[$right]]);
while ($left < $right && $nums[$left] == $nums[$left + 1]) $left++;
while ($left < $right && $nums[$right - 1] == $nums[$right]) $right--;
$left++;
$right--;
}
}
}
return $result;
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)

View File

@ -345,6 +345,40 @@ public:
Java
暴力法
```java
class Solution {
public int[] findMode(FindModeInBinarySearchTree.TreeNode root) {
Map<Integer, Integer> map = new HashMap<>();
List<Integer> list = new ArrayList<>();
if (root == null) return list.stream().mapToInt(Integer::intValue).toArray();
// 获得频率 Map
searchBST(root, map);
List<Map.Entry<Integer, Integer>> mapList = map.entrySet().stream()
.sorted((c1, c2) -> c2.getValue().compareTo(c1.getValue()))
.collect(Collectors.toList());
list.add(mapList.get(0).getKey());
// 把频率最高的加入 list
for (int i = 1; i < mapList.size(); i++) {
if (mapList.get(i).getValue() == mapList.get(i - 1).getValue()) {
list.add(mapList.get(i).getKey());
} else {
break;
}
}
return list.stream().mapToInt(Integer::intValue).toArray();
}
void searchBST(FindModeInBinarySearchTree.TreeNode curr, Map<Integer, Integer> map) {
if (curr == null) return;
map.put(curr.val, map.getOrDefault(curr.val, 0) + 1);
searchBST(curr.left, map);
searchBST(curr.right, map);
}
}
```
```Java
class Solution {
ArrayList<Integer> resList;

View File

@ -271,6 +271,9 @@ class Solution:
Go
递归法
```Go
func insertIntoBST(root *TreeNode, val int) *TreeNode {
if root == nil {
@ -285,6 +288,31 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode {
return root
}
```
迭代法
```go
func insertIntoBST(root *TreeNode, val int) *TreeNode {
if root == nil {
return &TreeNode{Val:val}
}
node := root
var pnode *TreeNode
for node != nil {
if val > node.Val {
pnode = node
node = node.Right
} else {
pnode = node
node = node.Left
}
}
if val > pnode.Val {
pnode.Right = &TreeNode{Val: val}
} else {
pnode.Left = &TreeNode{Val: val}
}
return root
}
```
JavaScript版本

View File

@ -381,6 +381,12 @@ dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]);
dp[i][2] = dp[i - 1][0] + prices[i];
dp[i][3] = dp[i - 1][2];
```
```C++
dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3]- prices[i], dp[i - 1][1]) - prices[i];
dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]);
dp[i][2] = dp[i - 1][0] + prices[i];
dp[i][3] = dp[i - 1][2];
```
整体代码如下: