Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d52f45e

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents 0acd924 + c41cb46 commit d52f45e

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed

‎problems/0001.两数之和.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,23 @@ var twoSum = function (nums, target) {
187187
};
188188
```
189189

190-
190+
php
191+
192+
```php
193+
function twoSum(array $nums, int $target): array
194+
{
195+
for ($i = 0; $i < count($nums);$i++) {
196+
// 计算剩下的数
197+
$residue = $target - $nums[$i];
198+
// 匹配的index,有则返回index, 无则返回false
199+
$match_index = array_search($residue, $nums);
200+
if ($match_index !== false && $match_index != $i) {
201+
return array($i, $match_index);
202+
}
203+
}
204+
return [];
205+
}
206+
```
191207

192208

193209
-----------------------

‎problems/0015.三数之和.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,47 @@ def is_valid(strs)
354354
end
355355
```
356356

357+
php:
358+
359+
```php
360+
function threeSum(array $nums): array
361+
{
362+
$result = [];
363+
$length = count($nums);
364+
if ($length < 3) {
365+
return [];
366+
}
367+
sort($nums);
368+
for ($i = 0; $i < $length; $i++) {
369+
// 如果大于0结束
370+
if ($nums[$i] > 0) break;
371+
// 去重
372+
if ($i > 0 && $nums[$i] == $nums[$i - 1]) continue;
373+
$left = $i + 1;
374+
$right = $length - 1;
375+
// 比较
376+
while ($left < $right) {
377+
$sum = $nums[$i] + $nums[$left] + $nums[$right];
378+
if ($sum < 0) {
379+
$left++;
380+
} elseif ($sum > 0) {
381+
$right--;
382+
} else {
383+
array_push($result, [$nums[$i], $nums[$left], $nums[$right]]);
384+
while ($left < $right && $nums[$left] == $nums[$left + 1]) $left++;
385+
while ($left < $right && $nums[$right - 1] == $nums[$right]) $right--;
386+
$left++;
387+
$right--;
388+
}
389+
}
390+
}
391+
392+
return $result;
393+
}
394+
```
395+
396+
397+
357398
-----------------------
358399
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
359400
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

‎problems/0501.二叉搜索树中的众数.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,40 @@ public:
345345

346346
Java:
347347

348+
暴力法
349+
```java
350+
class Solution {
351+
public int[] findMode(FindModeInBinarySearchTree.TreeNode root) {
352+
Map<Integer, Integer> map = new HashMap<>();
353+
List<Integer> list = new ArrayList<>();
354+
if (root == null) return list.stream().mapToInt(Integer::intValue).toArray();
355+
// 获得频率 Map
356+
searchBST(root, map);
357+
List<Map.Entry<Integer, Integer>> mapList = map.entrySet().stream()
358+
.sorted((c1, c2) -> c2.getValue().compareTo(c1.getValue()))
359+
.collect(Collectors.toList());
360+
list.add(mapList.get(0).getKey());
361+
// 把频率最高的加入 list
362+
for (int i = 1; i < mapList.size(); i++) {
363+
if (mapList.get(i).getValue() == mapList.get(i - 1).getValue()) {
364+
list.add(mapList.get(i).getKey());
365+
} else {
366+
break;
367+
}
368+
}
369+
return list.stream().mapToInt(Integer::intValue).toArray();
370+
}
371+
372+
void searchBST(FindModeInBinarySearchTree.TreeNode curr, Map<Integer, Integer> map) {
373+
if (curr == null) return;
374+
map.put(curr.val, map.getOrDefault(curr.val, 0) + 1);
375+
searchBST(curr.left, map);
376+
searchBST(curr.right, map);
377+
}
378+
379+
}
380+
```
381+
348382
```Java
349383
class Solution {
350384
ArrayList<Integer> resList;

‎problems/0701.二叉搜索树中的插入操作.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ class Solution:
271271

272272

273273
Go:
274+
275+
递归法
276+
274277
```Go
275278
func insertIntoBST(root *TreeNode, val int) *TreeNode {
276279
if root == nil {
@@ -285,6 +288,31 @@ func insertIntoBST(root *TreeNode, val int) *TreeNode {
285288
return root
286289
}
287290
```
291+
迭代法
292+
```go
293+
func insertIntoBST(root *TreeNode, val int) *TreeNode {
294+
if root == nil {
295+
return &TreeNode{Val:val}
296+
}
297+
node := root
298+
var pnode *TreeNode
299+
for node != nil {
300+
if val > node.Val {
301+
pnode = node
302+
node = node.Right
303+
} else {
304+
pnode = node
305+
node = node.Left
306+
}
307+
}
308+
if val > pnode.Val {
309+
pnode.Right = &TreeNode{Val: val}
310+
} else {
311+
pnode.Left = &TreeNode{Val: val}
312+
}
313+
return root
314+
}
315+
```
288316

289317
JavaScript版本
290318

‎problems/动态规划-股票问题总结篇.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,12 @@ dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]);
381381
dp[i][2] = dp[i - 1][0] + prices[i];
382382
dp[i][3] = dp[i - 1][2];
383383
```
384+
```C++
385+
dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3]- prices[i], dp[i - 1][1]) - prices[i];
386+
dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]);
387+
dp[i][2] = dp[i - 1][0] + prices[i];
388+
dp[i][3] = dp[i - 1][2];
389+
```
384390

385391
整体代码如下:
386392

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /