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 4ba8948

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents 4df16b4 + 391a04b commit 4ba8948

File tree

3 files changed

+129
-38
lines changed

3 files changed

+129
-38
lines changed

‎problems/0102.二叉树的层序遍历.md‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ var largestValues = function(root) {
11291129
queue.push(root);
11301130
while(root!==null&&queue.length){
11311131
//设置max初始值就是队列的第一个元素
1132-
let max=queue[0];
1132+
let max=queue[0].val;
11331133
let length=queue.length;
11341134
while(length--){
11351135
let node = queue.shift();
@@ -1532,13 +1532,38 @@ Java:
15321532
15331533
15341534
Python:
1535+
```python 3
1536+
class Solution:
1537+
def maxDepth(self, root: TreeNode) -> int:
1538+
if root == None:
1539+
return 0
1540+
1541+
queue_ = [root]
1542+
result = []
1543+
while queue_:
1544+
length = len(queue_)
1545+
sub = []
1546+
for i in range(length):
1547+
cur = queue_.pop(0)
1548+
sub.append(cur.val)
1549+
#子节点入队列
1550+
if cur.left: queue_.append(cur.left)
1551+
if cur.right: queue_.append(cur.right)
1552+
result.append(sub)
1553+
1554+
1555+
return len(result)
1556+
```
1557+
15351558

15361559
Go:
15371560

15381561
JavaScript:
15391562

15401563
# 111.二叉树的最小深度
15411564

1565+
题目地址:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
1566+
15421567
相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。
15431568

15441569
**需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点**
@@ -1574,7 +1599,35 @@ public:
15741599
Java:
15751600
15761601
1577-
Python:
1602+
Python 3:
1603+
1604+
```python 3
1605+
# Definition for a binary tree node.
1606+
# class TreeNode:
1607+
# def __init__(self, val=0, left=None, right=None):
1608+
# self.val = val
1609+
# self.left = left
1610+
# self.right = right
1611+
class Solution:
1612+
def minDepth(self, root: TreeNode) -> int:
1613+
if root == None:
1614+
return 0
1615+
1616+
#根节点的深度为1
1617+
queue_ = [(root,1)]
1618+
while queue_:
1619+
cur, depth = queue_.pop(0)
1620+
1621+
if cur.left == None and cur.right == None:
1622+
return depth
1623+
#先左子节点,由于左子节点没有孩子,则就是这一层了
1624+
if cur.left:
1625+
queue_.append((cur.left,depth + 1))
1626+
if cur.right:
1627+
queue_.append((cur.right,depth + 1))
1628+
1629+
return 0
1630+
```
15781631

15791632
Go:
15801633

‎problems/0188.买卖股票的最佳时机IV.md‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class Solution {
196196
}
197197
}
198198

199-
// 版本二: 空间优化
199+
// 版本二: 二维 dp数组
200200
class Solution {
201201
public int maxProfit(int k, int[] prices) {
202202
if (prices.length == 0) return 0;
@@ -220,6 +220,25 @@ class Solution {
220220
return dp[len - 1][k*2];
221221
}
222222
}
223+
224+
//版本三:一维 dp数组
225+
class Solution {
226+
public int maxProfit(int k, int[] prices) {
227+
//在版本二的基础上,由于我们只关心前一天的股票买入情况,所以只存储前一天的股票买入情况
228+
if(prices.length==0)return 0;
229+
int[] dp=new int[2*k+1];
230+
for (int i = 1; i <2*k ; i+=2) {
231+
dp[i]=-prices[0];
232+
}
233+
for (int i = 0; i <prices.length ; i++) {
234+
for (int j = 1; j <2*k ; j+=2) {
235+
dp[j]=Math.max(dp[j],dp[j-1]-prices[i]);
236+
dp[j+1]=Math.max(dp[j+1],dp[j]+prices[i]);
237+
}
238+
}
239+
return dp[2*k];
240+
}
241+
}
223242
```
224243

225244

‎problems/0707.设计链表.md‎

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -948,72 +948,91 @@ class MyLinkedList {
948948
}
949949
```
950950

951-
Swift
952-
```Swift
951+
952+
Swift:
953+
954+
```swift
953955
class MyLinkedList {
954-
var size =0
955-
let head: ListNode
956+
var dummyHead: ListNode<Int>?
957+
var size: Int
956958

957-
/** Initialize your data structure here. */
958959
init() {
959-
head = ListNode(-1)
960+
dummyHead = ListNode(0)
961+
size = 0
960962
}
961963

962-
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
963964
func get(_ index: Int) -> Int {
964-
if size > 0 && index < size {
965-
var tempHead = head
966-
for _ in 0..<index {
967-
tempHead = tempHead.next!
968-
}
969-
let currentNode = tempHead.next!
970-
return currentNode.val
971-
} else {
965+
if index >= size || index < 0 {
972966
return -1
973967
}
968+
969+
var curNode = dummyHead?.next
970+
var curIndex = index
971+
972+
while curIndex > 0 {
973+
curNode = curNode?.next
974+
curIndex -= 1
975+
}
976+
977+
return curNode?.value ?? -1
974978
}
975979

976-
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
977980
func addAtHead(_ val: Int) {
978-
addAtIndex(0, val)
981+
let newHead = ListNode(val)
982+
newHead.next = dummyHead?.next
983+
dummyHead?.next = newHead
984+
size += 1
979985
}
980986

981-
/** Append a node of value val to the last element of the linked list. */
982987
func addAtTail(_ val: Int) {
983-
addAtIndex(size, val)
988+
let newNode = ListNode(val)
989+
var curNode = dummyHead
990+
while curNode?.next != nil {
991+
curNode = curNode?.next
992+
}
993+
994+
curNode?.next = newNode
995+
size += 1
984996
}
985997

986-
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
987998
func addAtIndex(_ index: Int, _ val: Int) {
988999
if index > size {
9891000
return
9901001
}
991-
let idx = (index >= 0 ? index : 0)
992-
var tempHead = head
993-
for _ in 0 ..< idx {
994-
tempHead = tempHead.next!
1002+
1003+
let newNode = ListNode(val)
1004+
var curNode = dummyHead
1005+
var curIndex = index
1006+
1007+
while curIndex > 0 {
1008+
curNode = curNode?.next
1009+
curIndex -= 1
9951010
}
996-
let currentNode = tempHead.next
997-
letnewNode = ListNode(val, currentNode)
998-
tempHead.next = newNode
1011+
1012+
newNode.next = curNode?.next
1013+
curNode?.next = newNode
9991014
size += 1
10001015
}
10011016

1002-
/** Delete the index-th node in the linked list, if the index is valid. */
10031017
func deleteAtIndex(_ index: Int) {
1004-
if size >0&& index < size {
1005-
var tempHead = head
1006-
for_in0..< index {
1007-
tempHead = tempHead.next!
1008-
}
1009-
tempHead.next= tempHead.next!.next
1010-
size -=1
1018+
if index >= size || index < 0 {
1019+
return
1020+
}
1021+
1022+
var curNode = dummyHead
1023+
for_in0..<index {
1024+
curNode = curNode?.next
10111025
}
1026+
1027+
curNode?.next = curNode?.next?.next
1028+
size -= 1
10121029
}
10131030
}
10141031
```
10151032

10161033

1034+
1035+
10171036
-----------------------
10181037
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
10191038
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

0 commit comments

Comments
(0)

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