mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -139,29 +139,29 @@ public:
|
||||
## 迭代法
|
||||
|
||||
```C++
|
||||
lass Solution {
|
||||
class Solution {
|
||||
public:
|
||||
|
||||
bool isSameTree(TreeNode* p, TreeNode* q) {
|
||||
if (p == NULL && q == NULL) return true;
|
||||
if (p == NULL || q == NULL) return false;
|
||||
queue<TreeNode*> que;
|
||||
que.push(p); //
|
||||
que.push(q); //
|
||||
que.push(p); // 添加根节点p
|
||||
que.push(q); // 添加根节点q
|
||||
while (!que.empty()) { //
|
||||
TreeNode* leftNode = que.front(); que.pop();
|
||||
TreeNode* rightNode = que.front(); que.pop();
|
||||
if (!leftNode && !rightNode) { //
|
||||
if (!leftNode && !rightNode) { // 若p的节点与q的节点都为空
|
||||
continue;
|
||||
}
|
||||
//
|
||||
// 若p的节点与q的节点有一个为空或p的节点的值与q节点不同
|
||||
if ((!leftNode || !rightNode || (leftNode->val != rightNode->val))) {
|
||||
return false;
|
||||
}
|
||||
que.push(leftNode->left); //
|
||||
que.push(rightNode->left); //
|
||||
que.push(leftNode->right); //
|
||||
que.push(rightNode->right); //
|
||||
que.push(leftNode->left); // 添加p节点的左子树节点
|
||||
que.push(rightNode->left); // 添加q节点的左子树节点点
|
||||
que.push(leftNode->right); // 添加p节点的右子树节点
|
||||
que.push(rightNode->right); // 添加q节点的右子树节点
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -173,23 +173,71 @@ public:
|
||||
Java:
|
||||
|
||||
```java
|
||||
// 递归法
|
||||
class Solution {
|
||||
public boolean isSameTree(TreeNode p, TreeNode q) {
|
||||
if (p == null && q == null) {
|
||||
return true;
|
||||
} else if (q == null || p == null) {
|
||||
return false;
|
||||
} else if (q.val != p.val) {
|
||||
return false;
|
||||
} else {
|
||||
if (p == null && q == null) return true;
|
||||
else if (q == null || p == null) return false;
|
||||
else if (q.val != p.val) return false;
|
||||
return isSameTree(q.left, p.left) && isSameTree(q.right, p.right);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
// 迭代法
|
||||
class Solution {
|
||||
public boolean isSameTree(TreeNode p, TreeNode q) {
|
||||
if(p == null && q == null) return true;
|
||||
if(p == null || q == null) return false;
|
||||
Queue<TreeNode> que= new LinkedList<TreeNode>();
|
||||
que.offer(p);
|
||||
que.offer(q);
|
||||
while(!que.isEmpty()){
|
||||
TreeNode leftNode = que.poll();
|
||||
TreeNode rightNode = que.poll();
|
||||
if(leftNode == null && rightNode == null) continue;
|
||||
if(leftNode == null || rightNode== null || leftNode.val != rightNode.val) return false;
|
||||
que.offer(leftNode.left);
|
||||
que.offer(rightNode.left);
|
||||
que.offer(leftNode.right);
|
||||
que.offer(rightNode.right);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
Python:
|
||||
```python
|
||||
# 递归法
|
||||
class Solution:
|
||||
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
|
||||
if not p and not q: return True
|
||||
elif not p or not q: return False
|
||||
elif p.val != q.val: return False
|
||||
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
|
||||
```
|
||||
|
||||
```python
|
||||
# 迭代法
|
||||
class Solution:
|
||||
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
|
||||
if not p and not q: return True
|
||||
if not p or not q: return False
|
||||
que = collections.deque()
|
||||
que.append(p)
|
||||
que.append(q)
|
||||
while que:
|
||||
leftNode = que.popleft()
|
||||
rightNode = que.popleft()
|
||||
if not leftNode and not rightNode: continue
|
||||
if not leftNode or not rightNode or leftNode.val != rightNode.val: return False
|
||||
que.append(leftNode.left)
|
||||
que.append(rightNode.left)
|
||||
que.append(leftNode.right)
|
||||
que.append(rightNode.right)
|
||||
return True
|
||||
```
|
||||
Go:
|
||||
|
||||
JavaScript:
|
||||
|
@ -313,6 +313,26 @@ func max(a,b int)int {
|
||||
}
|
||||
```
|
||||
|
||||
JavaScript:
|
||||
|
||||
```javascript
|
||||
const maxProfit = prices => {
|
||||
const len = prices.length;
|
||||
// 创建dp数组
|
||||
const dp = new Array(len).fill([0, 0]);
|
||||
// dp数组初始化
|
||||
dp[0] = [-prices[0], 0];
|
||||
for (let i = 1; i < len; i++) {
|
||||
// 更新dp[i]
|
||||
dp[i] = [
|
||||
Math.max(dp[i - 1][0], -prices[i]),
|
||||
Math.max(dp[i - 1][1], prices[i] + dp[i - 1][0]),
|
||||
];
|
||||
}
|
||||
return dp[len - 1][1];
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -368,7 +368,32 @@ class Solution:
|
||||
return (val1, val2)
|
||||
```
|
||||
|
||||
Go:
|
||||
JavaScript:
|
||||
|
||||
> 动态规划
|
||||
|
||||
```javascript
|
||||
const rob = root => {
|
||||
// 后序遍历函数
|
||||
const postOrder = node => {
|
||||
// 递归出口
|
||||
if (!node) return [0, 0];
|
||||
// 遍历左子树
|
||||
const left = postOrder(node.left);
|
||||
// 遍历右子树
|
||||
const right = postOrder(node.right);
|
||||
// 不偷当前节点,左右子节点都可以偷或不偷,取最大值
|
||||
const DoNot = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
|
||||
// 偷当前节点,左右子节点只能不偷
|
||||
const Do = node.val + left[0] + right[0];
|
||||
// [不偷,偷]
|
||||
return [DoNot, Do];
|
||||
};
|
||||
const res = postOrder(root);
|
||||
// 返回最大值
|
||||
return Math.max(...res);
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -190,6 +190,101 @@ class Solution:
|
||||
Go:
|
||||
|
||||
|
||||
javaScript:
|
||||
```js
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @param {number} k
|
||||
* @return {number[]}
|
||||
*/
|
||||
var topKFrequent = function(nums, k) {
|
||||
const map = new Map();
|
||||
|
||||
for(const num of nums) {
|
||||
map.set(num, (map.get(num) || 0) + 1);
|
||||
}
|
||||
|
||||
// 创建小顶堆
|
||||
const priorityQueue = new PriorityQueue((a, b) => a[1] - b[1]);
|
||||
|
||||
// entry 是一个长度为2的数组,0位置存储key,1位置存储value
|
||||
for (const entry of map.entries()) {
|
||||
priorityQueue.push(entry);
|
||||
if (priorityQueue.size() > k) {
|
||||
priorityQueue.pop();
|
||||
}
|
||||
}
|
||||
|
||||
const ret = [];
|
||||
|
||||
for(let i = priorityQueue.size() - 1; i >= 0; i--) {
|
||||
ret[i] = priorityQueue.pop()[0];
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
|
||||
function PriorityQueue(compareFn) {
|
||||
this.compareFn = compareFn;
|
||||
this.queue = [];
|
||||
}
|
||||
|
||||
// 添加
|
||||
PriorityQueue.prototype.push = function(item) {
|
||||
this.queue.push(item);
|
||||
let index = this.queue.length - 1;
|
||||
let parent = Math.floor((index - 1) / 2);
|
||||
// 上浮
|
||||
while(parent >= 0 && this.compare(parent, index) > 0) {
|
||||
// 交换
|
||||
[this.queue[index], this.queue[parent]] = [this.queue[parent], this.queue[index]];
|
||||
index = parent;
|
||||
parent = Math.floor((index - 1) / 2);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取堆顶元素并移除
|
||||
PriorityQueue.prototype.pop = function() {
|
||||
const ret = this.queue[0];
|
||||
|
||||
// 把最后一个节点移到堆顶
|
||||
this.queue[0] = this.queue.pop();
|
||||
|
||||
let index = 0;
|
||||
// 左子节点下标,left + 1 就是右子节点下标
|
||||
let left = 1;
|
||||
let selectedChild = this.compare(left, left + 1) > 0 ? left + 1 : left;
|
||||
|
||||
// 下沉
|
||||
while(selectedChild !== undefined && this.compare(index, selectedChild) > 0) {
|
||||
// 交换
|
||||
[this.queue[index], this.queue[selectedChild]] = [this.queue[selectedChild], this.queue[index]];
|
||||
index = selectedChild;
|
||||
left = 2 * index + 1;
|
||||
selectedChild = this.compare(left, left + 1) > 0 ? left + 1 : left;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
PriorityQueue.prototype.size = function() {
|
||||
return this.queue.length;
|
||||
}
|
||||
|
||||
// 使用传入的 compareFn 比较两个位置的元素
|
||||
PriorityQueue.prototype.compare = function(index1, index2) {
|
||||
if (this.queue[index1] === undefined) {
|
||||
return 1;
|
||||
}
|
||||
if (this.queue[index2] === undefined) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return this.compareFn(this.queue[index1], this.queue[index2]);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
|
@ -255,7 +255,7 @@ class Solution {
|
||||
|
||||
Python:
|
||||
|
||||
递归法
|
||||
**递归法** - 有返回值
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
@ -268,7 +268,63 @@ class Solution:
|
||||
root.left = self.insertIntoBST(root.left, val) # 递归创建左子树
|
||||
return root
|
||||
```
|
||||
**递归法** - 无返回值
|
||||
```python
|
||||
class Solution:
|
||||
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
|
||||
if not root:
|
||||
return TreeNode(val)
|
||||
parent = None
|
||||
def __traverse(cur: TreeNode, val: int) -> None:
|
||||
# 在函数运行的同时把新节点插入到该被插入的地方.
|
||||
nonlocal parent
|
||||
if not cur:
|
||||
new_node = TreeNode(val)
|
||||
if parent.val < val:
|
||||
parent.right = new_node
|
||||
else:
|
||||
parent.left = new_node
|
||||
return
|
||||
|
||||
parent = cur # 重点: parent的作用只有运行到上面if not cur:才会发挥出来.
|
||||
if cur.val < val:
|
||||
__traverse(cur.right, val)
|
||||
else:
|
||||
__traverse(cur.left, val)
|
||||
return
|
||||
__traverse(root, val)
|
||||
return root
|
||||
```
|
||||
**迭代法**
|
||||
与无返回值的递归函数的思路大体一致
|
||||
```python
|
||||
class Solution:
|
||||
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
|
||||
if not root:
|
||||
return TreeNode(val)
|
||||
parent = None
|
||||
cur = root
|
||||
|
||||
# 用while循环不断地找新节点的parent
|
||||
while cur:
|
||||
if cur.val < val:
|
||||
parent = cur
|
||||
cur = cur.right
|
||||
elif cur.val > val:
|
||||
parent = cur
|
||||
cur = cur.left
|
||||
|
||||
# 运行到这意味着已经跳出上面的while循环,
|
||||
# 同时意味着新节点的parent已经被找到.
|
||||
# parent已被找到, 新节点已经ready. 把两个节点黏在一起就好了.
|
||||
if parent.val > val:
|
||||
parent.left = TreeNode(val)
|
||||
else:
|
||||
parent.right = TreeNode(val)
|
||||
|
||||
return root
|
||||
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
@ -311,7 +311,38 @@ func main() {
|
||||
fmt.Println(test_CompletePack2(weight, price, 4))
|
||||
}
|
||||
```
|
||||
Javascript:
|
||||
```Javascript
|
||||
// 先遍历物品,再遍历背包容量
|
||||
function test_completePack1() {
|
||||
let weight = [1, 3, 5]
|
||||
let value = [15, 20, 30]
|
||||
let bagWeight = 4
|
||||
let dp = new Array(bagWeight + 1).fill(0)
|
||||
for(let i = 0; i <= weight.length; i++) {
|
||||
for(let j = weight[i]; j <= bagWeight; j++) {
|
||||
dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i])
|
||||
}
|
||||
}
|
||||
console.log(dp)
|
||||
}
|
||||
|
||||
// 先遍历背包容量,再遍历物品
|
||||
function test_completePack2() {
|
||||
let weight = [1, 3, 5]
|
||||
let value = [15, 20, 30]
|
||||
let bagWeight = 4
|
||||
let dp = new Array(bagWeight + 1).fill(0)
|
||||
for(let j = 0; j <= bagWeight; j++) {
|
||||
for(let i = 0; i < weight.length; i++) {
|
||||
if (j >= weight[i]) {
|
||||
dp[j] = Math.max(dp[j], dp[j - weight[i]] + value[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(2, dp);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user