mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -1129,7 +1129,7 @@ var largestValues = function(root) {
|
||||
queue.push(root);
|
||||
while(root!==null&&queue.length){
|
||||
//设置max初始值就是队列的第一个元素
|
||||
let max=queue[0];
|
||||
let max=queue[0].val;
|
||||
let length=queue.length;
|
||||
while(length--){
|
||||
let node = queue.shift();
|
||||
@ -1532,6 +1532,29 @@ Java:
|
||||
|
||||
|
||||
Python:
|
||||
```python 3
|
||||
class Solution:
|
||||
def maxDepth(self, root: TreeNode) -> int:
|
||||
if root == None:
|
||||
return 0
|
||||
|
||||
queue_ = [root]
|
||||
result = []
|
||||
while queue_:
|
||||
length = len(queue_)
|
||||
sub = []
|
||||
for i in range(length):
|
||||
cur = queue_.pop(0)
|
||||
sub.append(cur.val)
|
||||
#子节点入队列
|
||||
if cur.left: queue_.append(cur.left)
|
||||
if cur.right: queue_.append(cur.right)
|
||||
result.append(sub)
|
||||
|
||||
|
||||
return len(result)
|
||||
```
|
||||
|
||||
|
||||
Go:
|
||||
|
||||
@ -1539,6 +1562,8 @@ JavaScript:
|
||||
|
||||
# 111.二叉树的最小深度
|
||||
|
||||
题目地址:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
|
||||
|
||||
相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。
|
||||
|
||||
**需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点**
|
||||
@ -1574,7 +1599,35 @@ public:
|
||||
Java:
|
||||
|
||||
|
||||
Python:
|
||||
Python 3:
|
||||
|
||||
```python 3
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def minDepth(self, root: TreeNode) -> int:
|
||||
if root == None:
|
||||
return 0
|
||||
|
||||
#根节点的深度为1
|
||||
queue_ = [(root,1)]
|
||||
while queue_:
|
||||
cur, depth = queue_.pop(0)
|
||||
|
||||
if cur.left == None and cur.right == None:
|
||||
return depth
|
||||
#先左子节点,由于左子节点没有孩子,则就是这一层了
|
||||
if cur.left:
|
||||
queue_.append((cur.left,depth + 1))
|
||||
if cur.right:
|
||||
queue_.append((cur.right,depth + 1))
|
||||
|
||||
return 0
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
@ -196,7 +196,7 @@ class Solution {
|
||||
}
|
||||
}
|
||||
|
||||
// 版本二: 空间优化
|
||||
// 版本二: 二维 dp数组
|
||||
class Solution {
|
||||
public int maxProfit(int k, int[] prices) {
|
||||
if (prices.length == 0) return 0;
|
||||
@ -220,6 +220,25 @@ class Solution {
|
||||
return dp[len - 1][k*2];
|
||||
}
|
||||
}
|
||||
|
||||
//版本三:一维 dp数组
|
||||
class Solution {
|
||||
public int maxProfit(int k, int[] prices) {
|
||||
//在版本二的基础上,由于我们只关心前一天的股票买入情况,所以只存储前一天的股票买入情况
|
||||
if(prices.length==0)return 0;
|
||||
int[] dp=new int[2*k+1];
|
||||
for (int i = 1; i <2*k ; i+=2) {
|
||||
dp[i]=-prices[0];
|
||||
}
|
||||
for (int i = 0; i <prices.length ; i++) {
|
||||
for (int j = 1; j <2*k ; j+=2) {
|
||||
dp[j]=Math.max(dp[j],dp[j-1]-prices[i]);
|
||||
dp[j+1]=Math.max(dp[j+1],dp[j]+prices[i]);
|
||||
}
|
||||
}
|
||||
return dp[2*k];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
@ -948,72 +948,91 @@ class MyLinkedList {
|
||||
}
|
||||
```
|
||||
|
||||
Swift
|
||||
```Swift
|
||||
|
||||
Swift:
|
||||
|
||||
```swift
|
||||
class MyLinkedList {
|
||||
var size = 0
|
||||
let head: ListNode
|
||||
var dummyHead: ListNode<Int>?
|
||||
var size: Int
|
||||
|
||||
/** Initialize your data structure here. */
|
||||
init() {
|
||||
head = ListNode(-1)
|
||||
dummyHead = ListNode(0)
|
||||
size = 0
|
||||
}
|
||||
|
||||
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
|
||||
func get(_ index: Int) -> Int {
|
||||
if size > 0 && index < size {
|
||||
var tempHead = head
|
||||
for _ in 0..<index {
|
||||
tempHead = tempHead.next!
|
||||
}
|
||||
let currentNode = tempHead.next!
|
||||
return currentNode.val
|
||||
} else {
|
||||
if index >= size || index < 0 {
|
||||
return -1
|
||||
}
|
||||
|
||||
var curNode = dummyHead?.next
|
||||
var curIndex = index
|
||||
|
||||
while curIndex > 0 {
|
||||
curNode = curNode?.next
|
||||
curIndex -= 1
|
||||
}
|
||||
|
||||
return curNode?.value ?? -1
|
||||
}
|
||||
|
||||
/** 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. */
|
||||
func addAtHead(_ val: Int) {
|
||||
addAtIndex(0, val)
|
||||
let newHead = ListNode(val)
|
||||
newHead.next = dummyHead?.next
|
||||
dummyHead?.next = newHead
|
||||
size += 1
|
||||
}
|
||||
|
||||
/** Append a node of value val to the last element of the linked list. */
|
||||
func addAtTail(_ val: Int) {
|
||||
addAtIndex(size, val)
|
||||
let newNode = ListNode(val)
|
||||
var curNode = dummyHead
|
||||
while curNode?.next != nil {
|
||||
curNode = curNode?.next
|
||||
}
|
||||
|
||||
curNode?.next = newNode
|
||||
size += 1
|
||||
}
|
||||
|
||||
/** 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. */
|
||||
func addAtIndex(_ index: Int, _ val: Int) {
|
||||
if index > size {
|
||||
return
|
||||
}
|
||||
let idx = (index >= 0 ? index : 0)
|
||||
var tempHead = head
|
||||
for _ in 0 ..< idx {
|
||||
tempHead = tempHead.next!
|
||||
|
||||
let newNode = ListNode(val)
|
||||
var curNode = dummyHead
|
||||
var curIndex = index
|
||||
|
||||
while curIndex > 0 {
|
||||
curNode = curNode?.next
|
||||
curIndex -= 1
|
||||
}
|
||||
let currentNode = tempHead.next
|
||||
let newNode = ListNode(val, currentNode)
|
||||
tempHead.next = newNode
|
||||
|
||||
newNode.next = curNode?.next
|
||||
curNode?.next = newNode
|
||||
size += 1
|
||||
}
|
||||
|
||||
/** Delete the index-th node in the linked list, if the index is valid. */
|
||||
func deleteAtIndex(_ index: Int) {
|
||||
if size > 0 && index < size {
|
||||
var tempHead = head
|
||||
for _ in 0 ..< index {
|
||||
tempHead = tempHead.next!
|
||||
if index >= size || index < 0 {
|
||||
return
|
||||
}
|
||||
tempHead.next = tempHead.next!.next
|
||||
|
||||
var curNode = dummyHead
|
||||
for _ in 0..<index {
|
||||
curNode = curNode?.next
|
||||
}
|
||||
|
||||
curNode?.next = curNode?.next?.next
|
||||
size -= 1
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
|
Reference in New Issue
Block a user