Merge branch 'master' of github.com:youngyangyang04/leetcode-master

This commit is contained in:
programmercarl
2023-11-24 09:47:03 +08:00
20 changed files with 665 additions and 40 deletions

View File

@ -397,9 +397,9 @@
* [图论417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md)
* [图论827.最大人工岛](./problems/0827.最大人工岛.md)
* [图论127. 单词接龙](./problems/0127.单词接龙.md)
* [图论841.钥匙和房间](./problems/841.钥匙和房间)
* [图论841.钥匙和房间](./problems/0841.钥匙和房间.md)
* [图论463. 岛屿的周长](./problems/0463.岛屿的周长.md)
* [图论:并查集理论基础](./problems/)
* [图论:并查集理论基础](./problems/图论并查集理论基础.md)
* [图论1971. 寻找图中是否存在路径](./problems/1971.寻找图中是否存在路径.md)
* [图论684.冗余连接](./problems/0684.冗余连接.md)
* [图论685.冗余连接II](./problems/0685.冗余连接II.md)

View File

@ -100,8 +100,8 @@ int main() {
while (cin >> n >> m) {
vector<int> dp(n + 1, 0);
dp[0] = 1;
for (int i = 1; i <= n; i++) { // 遍历物品
for (int j = 1; j <= m; j++) { // 遍历背包
for (int i = 1; i <= n; i++) { // 遍历背包
for (int j = 1; j <= m; j++) { // 遍历物品
if (i - j >= 0) dp[i] += dp[i - j];
}
}

View File

@ -897,6 +897,53 @@ impl Solution {
}
}
```
### C#
```C#
// 递归
public bool IsSymmetric(TreeNode root)
{
if (root == null) return true;
return Compare(root.left, root.right);
}
public bool Compare(TreeNode left, TreeNode right)
{
if(left == null && right != null) return false;
else if(left != null && right == null ) return false;
else if(left == null && right == null) return true;
else if(left.val != right.val) return false;
var outside = Compare(left.left, right.right);
var inside = Compare(left.right, right.left);
return outside&&inside;
}
```
``` C#
// 迭代法
public bool IsSymmetric(TreeNode root)
{
if (root == null) return true;
var st = new Stack<TreeNode>();
st.Push(root.left);
st.Push(root.right);
while (st.Count != 0)
{
var left = st.Pop();
var right = st.Pop();
if (left == null && right == null)
continue;
if ((left == null || right == null || (left.val != right.val)))
return false;
st.Push(left.left);
st.Push(right.right);
st.Push(left.right);
st.Push(right.left);
}
return true;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -462,6 +462,32 @@ impl Solution {
}
}
```
### C#
```C#
public IList<IList<int>> LevelOrder(TreeNode root)
{
var res = new List<IList<int>>();
var que = new Queue<TreeNode>();
if (root == null) return res;
que.Enqueue(root);
while (que.Count != 0)
{
var size = que.Count;
var vec = new List<int>();
for (int i = 0; i < size; i++)
{
var cur = que.Dequeue();
vec.Add(cur.val);
if (cur.left != null) que.Enqueue(cur.left);
if (cur.right != null) que.Enqueue(cur.right);
}
res.Add(vec);
}
return res;
}
```
**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
@ -798,6 +824,31 @@ impl Solution {
}
}
```
### C#
```C#
public IList<IList<int>> LevelOrderBottom(TreeNode root)
{
var res = new List<IList<int>>();
var que = new Queue<TreeNode>();
if (root == null) return res;
que.Enqueue(root);
while (que.Count != 0)
{
var size = que.Count;
var vec = new List<int>();
for (int i = 0; i < size; i++)
{
var cur = que.Dequeue();
vec.Add(cur.val);
if (cur.left != null) que.Enqueue(cur.left);
if (cur.right != null) que.Enqueue(cur.right);
}
res.Add(vec);
}
res.Reverse();
return res;
}
```
## 199.二叉树的右视图

View File

@ -738,26 +738,36 @@ function maxDepth(root: TreeNode | null): number {
```typescript
// 后续遍历(自下而上)
function maxDepth(root: TreeNode | null): number {
if (root === null) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
};
function maxDepth(root: Node | null): number {
if (root === null) return 0
let depth = 0
for (let i = 0; i < root.children.length; i++) {
depth = Math.max(depth, maxDepth(root.children[i]))
}
return depth + 1
}
// 前序遍历(自上而下)
function maxDepth(root: TreeNode | null): number {
function recur(node: TreeNode | null, count: number) {
if (node === null) {
resMax = resMax > count ? resMax : count;
return;
function maxDepth(root: Node | null): number {
if (root === null) return 0
let depth: number = 0
const queue: Array<Node | null> = []
queue.push(root)
while (queue.length > 0) {
let len = queue.length
depth++
for (let i = 0; i < len; i++) {
// 当前层遍历
let curNode: Node | null = queue.shift()!
for (let j = 0; j < curNode.children.length; j++) {
if (curNode.children[j]) queue.push(curNode.children[j])
}
}
recur(node.left, count + 1);
recur(node.right, count + 1);
}
let resMax: number = 0;
let count: number = 0;
recur(root, count);
return resMax;
};
return depth
}
```
@ -1022,6 +1032,61 @@ impl Solution {
max_depth
}
```
### C#
```C#
// 递归法
public int MaxDepth(TreeNode root) {
if(root == null) return 0;
int leftDepth = MaxDepth(root.left);
int rightDepth = MaxDepth(root.right);
return 1 + Math.Max(leftDepth, rightDepth);
}
```
```C#
// 前序遍历
int result = 0;
public int MaxDepth(TreeNode root)
{
if (root == null) return result;
GetDepth(root, 1);
return result;
}
public void GetDepth(TreeNode root, int depth)
{
result = depth > result ? depth : result;
if (root.left == null && root.right == null) return;
if (root.left != null)
GetDepth(root.left, depth + 1);
if (root.right != null)
GetDepth(root.right, depth + 1);
return;
}
```
```C#
// 迭代法
public int MaxDepth(TreeNode root)
{
int depth = 0;
Queue<TreeNode> que = new();
if (root == null) return depth;
que.Enqueue(root);
while (que.Count != 0)
{
int size = que.Count;
depth++;
for (int i = 0; i < size; i++)
{
var node = que.Dequeue();
if (node.left != null) que.Enqueue(node.left);
if (node.right != null) que.Enqueue(node.right);
}
}
return depth;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -908,6 +908,31 @@ impl Solution {
}
}
```
### C#
```C#
public bool IsBalanced(TreeNode root)
{
return GetHeight(root) == -1 ? false : true;
}
public int GetHeight(TreeNode root)
{
if (root == null) return 0;
int left = GetHeight(root.left);
if (left == -1) return -1;
int right = GetHeight(root.right);
if (right == -1) return -1;
int res;
if (Math.Abs(left - right) > 1)
{
res = -1;
}
else
{
res = 1 + Math.Max(left, right);
}
return res;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -708,6 +708,49 @@ impl Solution {
}
}
```
### C#
```C#
// 递归
public int MinDepth(TreeNode root)
{
if (root == null) return 0;
int left = MinDepth(root.left);
int right = MinDepth(root.right);
if (root.left == null && root.right != null)
return 1+right;
else if(root.left!=null && root.right == null)
return 1+left;
int res = 1 + Math.Min(left, right);
return res;
}
```
```C#
// 迭代
public int MinDepth(TreeNode root)
{
if (root == null) return 0;
int depth = 0;
var que = new Queue<TreeNode>();
que.Enqueue(root);
while (que.Count > 0)
{
int size = que.Count;
depth++;
for (int i = 0; i < size; i++)
{
var node = que.Dequeue();
if (node.left != null)
que.Enqueue(node.left);
if (node.right != null)
que.Enqueue(node.right);
if (node.left == null && node.right == null)
return depth;
}
}
return depth;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -1511,6 +1511,17 @@ impl Solution {
}
}
```
### C#
```C#
// 0112.路径总和
// 递归
public bool HasPathSum(TreeNode root, int targetSum)
{
if (root == null) return false;
if (root.left == null && root.right == null && targetSum == root.val) return true;
return HasPathSum(root.left, targetSum - root.val) || HasPathSum(root.right, targetSum - root.val);
}
```
<p align="center">

View File

@ -867,6 +867,31 @@ impl Solution {
}
}
```
### C#
```C#
// 递归
public int CountNodes(TreeNode root)
{
if (root == null) return 0;
var left = root.left;
var right = root.right;
int leftDepth = 0, rightDepth = 0;
while (left != null)
{
left = left.left;
leftDepth++;
}
while (right != null)
{
right = right.right;
rightDepth++;
}
if (leftDepth == rightDepth)
return (int)Math.Pow(2, leftDepth+1) - 1;
return CountNodes(root.left) + CountNodes(root.right) + 1;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -1018,25 +1018,13 @@ public class Solution {
```csharp
//迭代
public class Solution {
public TreeNode InvertTree(TreeNode root) {
if (root == null) return null;
Stack<TreeNode> stack=new Stack<TreeNode>();
stack.Push(root);
while(stack.Count>0)
{
TreeNode node = stack.Pop();
swap(node);
if(node.right!=null) stack.Push(node.right);
if(node.left!=null) stack.Push(node.left);
}
return root;
}
public void swap(TreeNode node) {
TreeNode temp = node.left;
node.left = node.right;
node.right = temp;
}
public TreeNode InvertTree(TreeNode root) {
if(root == null) return root;
(root.left,root.right) = (root.right, root.left);
InvertTree(root.left);
InvertTree(root.right);
return root;
}
}
```

View File

@ -900,6 +900,43 @@ impl Solution {
}
}
```
### C#
```C#
public IList<string> BinaryTreePaths(TreeNode root)
{
List<int> path = new();
List<string> res = new();
if (root == null) return res;
Traversal(root, path, res);
return res;
}
public void Traversal(TreeNode node, List<int> path, List<string> res)
{
path.Add(node.val);
if (node.left == null && node.right == null)
{
string sPath = "";
for (int i = 0; i < path.Count - 1; i++)
{
sPath += path[i].ToString();
sPath += "->";
}
sPath += path[path.Count - 1].ToString();
res.Add(sPath);
return;
}
if (node.left != null)
{
Traversal(node.left, path, res);
path.RemoveAt(path.Count-1);
}
if (node.right != null)
{
Traversal(node.right, path, res);
path.RemoveAt(path.Count-1);
}
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -250,6 +250,20 @@ class Solution:
s[:] = [s[i] for i in range(len(s) - 1, -1, -1)]
```
(版本七) 使用reverse()
```python
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
# 原地反转,无返回值
s.reverse()
```
### Go
```Go

View File

@ -214,6 +214,19 @@ class Solution:
return all(ransomNote.count(c) <= magazine.count(c) for c in set(ransomNote))
```
(版本六使用count(简单易懂)
```python3
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
for char in ransomNote:
if char in magazine and ransomNote.count(char) <= magazine.count(char):
continue
else:
return False
return True
```
### Go
```go

View File

@ -651,6 +651,23 @@ impl Solution {
}
}
```
### C#
```C#
// 递归
public int SumOfLeftLeaves(TreeNode root)
{
if (root == null) return 0;
int leftValue = SumOfLeftLeaves(root.left);
if (root.left != null && root.left.left == null && root.left.right == null)
{
leftValue += root.left.val;
}
int rightValue = SumOfLeftLeaves(root.right);
return leftValue + rightValue;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -684,6 +684,38 @@ impl Solution {
}
}
```
### C#
```C#
//递归
int maxDepth = -1;
int res = 0;
public int FindBottomLeftValue(TreeNode root)
{
Traversal(root, 0);
return res;
}
public void Traversal(TreeNode root, int depth)
{
if (root.left == null && root.right == null)
{
if (depth > maxDepth)
{
maxDepth = depth;
res = root.val;
}
return;
}
if (root.left != null)
{
Traversal(root.left, depth + 1);
}
if (root.right != null)
{
Traversal(root.right, depth + 1);
}
return;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -46,7 +46,7 @@
从前向后填充就是O(n^2)的算法了因为每次添加元素都要将添加元素之后的所有元素整体向后移动
**其实很多数组填充类的问题,其做都是先预先给数组扩容带填充后的大小,然后在从后向前进行操作。**
**其实很多数组填充类的问题,其做都是先预先给数组扩容带填充后的大小,然后在从后向前进行操作。**
这么做有两个好处
@ -162,6 +162,27 @@ class Main {
```
### Go
````go
package main
import "fmt"
func main(){
var strByte []byte
fmt.Scanln(&strByte)
for i := 0; i < len(strByte); i++{
if strByte[i] <= '9' && strByte[i] >= '0' {
inserElement := []byte{'n','u','m','b','e','r'}
strByte = append(strByte[:i], append(inserElement, strByte[i+1:]...)...)
i = i + len(inserElement) -1
}
}
fmt.Printf(string(strByte))
}
````

View File

@ -214,6 +214,34 @@ public class Main {
### Go
```go
package main
import "fmt"
func reverse (strByte []byte, l, r int){
for l < r {
strByte[l], strByte[r] = strByte[r], strByte[l]
l++
r--
}
}
func main(){
var str string
var target int
fmt.Scanln(&target)
fmt.Scanln(&str)
strByte := []byte(str)
reverse(strByte, 0, len(strByte) - 1)
reverse(strByte, 0, target - 1)
reverse(strByte, target, len(strByte) - 1)
fmt.Printf(string(strByte))
}
```
### JavaScript

View File

@ -741,6 +741,99 @@ impl Solution{
}
}
```
### C#
```C#
// 前序遍历
public IList<int> PreorderTraversal(TreeNode root)
{
var res = new List<int>();
var st = new Stack<TreeNode>();
if (root == null) return res;
st.Push(root);
while (st.Count != 0)
{
var node = st.Peek();
if (node == null)
{
st.Pop();
node = st.Peek();
st.Pop();
res.Add(node.val);
}
else
{
st.Pop();
if (node.right != null) st.Push(node.right);
if (node.left != null) st.Push(node.left);
st.Push(node);
st.Push(null);
}
}
return res;
}
```
```C#
// 中序遍历
public IList<int> InorderTraversal(TreeNode root)
{
var res = new List<int>();
var st = new Stack<TreeNode>();
if (root == null) return res;
st.Push(root);
while (st.Count != 0)
{
var node = st.Peek();
if (node == null)
{
st.Pop();
node = st.Peek();
st.Pop();
res.Add(node.val);
}
else
{
st.Pop();
if (node.right != null) st.Push(node.right);
st.Push(node);
st.Push(null);
if (node.left != null) st.Push(node.left);
}
}
return res;
}
```
```C#
// 后序遍历
public IList<int> PostorderTraversal(TreeNode root)
{
var res = new List<int>();
var st = new Stack<TreeNode>();
if (root == null) return res;
st.Push(root);
while (st.Count != 0)
{
var node = st.Peek();
if (node == null)
{
st.Pop();
node = st.Peek();
st.Pop();
res.Add(node.val);
}
else
{
st.Pop();
if (node.left != null) st.Push(node.left);
if (node.right != null) st.Push(node.right);
st.Push(node);
st.Push(null);
}
}
res.Reverse(0, res.Count);
return res;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -695,6 +695,67 @@ impl Solution {
}
}
```
### C#
```C#
// 前序遍历
public IList<int> PreorderTraversal(TreeNode root)
{
var st = new Stack<TreeNode>();
var res = new List<int>();
if (root == null) return res;
st.Push(root);
while (st.Count != 0)
{
var node = st.Pop();
res.Add(node.val);
if (node.right != null)
st.Push(node.right);
if (node.left != null)
st.Push(node.left);
}
return res;
}
// 中序遍历
public IList<int> InorderTraversal(TreeNode root)
{
var st = new Stack<TreeNode>();
var res = new List<int>();
var cur = root;
while (st.Count != 0 || cur != null)
{
if (cur != null)
{
st.Push(cur);
cur = cur.left;
}
else
{
cur = st.Pop();
res.Add(cur.val);
cur = cur.right;
}
}
return res;
}
// 后序遍历
public IList<int> PostorderTraversal(TreeNode root)
{
var res = new List<int>();
var st = new Stack<TreeNode>();
if (root == null) return res;
st.Push(root);
while (st.Count != 0)
{
var cur = st.Pop();
res.Add(cur.val);
if (cur.left != null) st.Push(cur.left);
if (cur.right != null) st.Push(cur.right);
}
res.Reverse(0, res.Count());
return res;
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

View File

@ -565,6 +565,60 @@ impl Solution {
}
```
### C#
```C#
// 前序遍历
public IList<int> PreorderTraversal(TreeNode root)
{
var res = new List<int>();
if (root == null) return res;
Traversal(root, res);
return res;
}
public void Traversal(TreeNode cur, IList<int> res)
{
if (cur == null) return;
res.Add(cur.val);
Traversal(cur.left, res);
Traversal(cur.right, res);
}
```
```C#
// 中序遍历
public IList<int> InorderTraversal(TreeNode root)
{
var res = new List<int>();
if (root == null) return res;
Traversal(root, res);
return res;
}
public void Traversal(TreeNode cur, IList<int> res)
{
if (cur == null) return;
Traversal(cur.left, res);
res.Add(cur.val);
Traversal(cur.right, res);
}
```
```C#
// 后序遍历
public IList<int> PostorderTraversal(TreeNode root)
{
var res = new List<int>();
if (root == null) return res;
Traversal(root, res);
return res;
}
public void Traversal(TreeNode cur, IList<int> res)
{
if (cur == null) return;
Traversal(cur.left, res);
Traversal(cur.right, res);
res.Add(cur.val);
}
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>