diff --git a/README.md b/README.md index c1296ed3..15b10bfa 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/problems/0070.爬楼梯完全背包版本.md b/problems/0070.爬楼梯完全背包版本.md index 0da1ebec..26af2569 100644 --- a/problems/0070.爬楼梯完全背包版本.md +++ b/problems/0070.爬楼梯完全背包版本.md @@ -100,8 +100,8 @@ int main() { while (cin >> n >> m) { vector 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]; } } diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 36b39740..1b777dd5 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -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(); + 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; +} +```

diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 51d030e7..e977279c 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -462,6 +462,32 @@ impl Solution { } } ``` +### C# +```C# +public IList> LevelOrder(TreeNode root) +{ + var res = new List>(); + var que = new Queue(); + if (root == null) return res; + que.Enqueue(root); + while (que.Count != 0) + { + var size = que.Count; + var vec = new List(); + 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> LevelOrderBottom(TreeNode root) +{ + var res = new List>(); + var que = new Queue(); + if (root == null) return res; + que.Enqueue(root); + while (que.Count != 0) + { + var size = que.Count; + var vec = new List(); + 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.二叉树的右视图 diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 05044375..b89dbf45 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -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 = [] + 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 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; +} +```

diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index c7df9c8f..41669bff 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -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; +} +```

diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md index c4e55a07..b27df4a6 100644 --- a/problems/0111.二叉树的最小深度.md +++ b/problems/0111.二叉树的最小深度.md @@ -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(); + 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; +} +```

diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index be03f719..f1ce7637 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -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); +} ```

diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index d54f9b85..ef6e88aa 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -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; + +} +```

diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index 11517783..8691953a 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -1018,25 +1018,13 @@ public class Solution { ```csharp //迭代 public class Solution { - public TreeNode InvertTree(TreeNode root) { - if (root == null) return null; - Stack stack=new Stack(); - 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; +} } ``` diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md index 44c0fd85..9ba165c7 100644 --- a/problems/0257.二叉树的所有路径.md +++ b/problems/0257.二叉树的所有路径.md @@ -900,6 +900,43 @@ impl Solution { } } ``` +### C# +```C# +public IList BinaryTreePaths(TreeNode root) +{ + List path = new(); + List res = new(); + if (root == null) return res; + Traversal(root, path, res); + return res; +} +public void Traversal(TreeNode node, List path, List 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); + } +} +```

diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index 8a4fed45..44184c53 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -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 diff --git a/problems/0383.赎金信.md b/problems/0383.赎金信.md index 3de48ce3..b800c232 100644 --- a/problems/0383.赎金信.md +++ b/problems/0383.赎金信.md @@ -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 diff --git a/problems/0404.左叶子之和.md b/problems/0404.左叶子之和.md index c1ad602d..6dfcc886 100644 --- a/problems/0404.左叶子之和.md +++ b/problems/0404.左叶子之和.md @@ -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; + +} +```

diff --git a/problems/0513.找树左下角的值.md b/problems/0513.找树左下角的值.md index 7ef934cc..0e2f4266 100644 --- a/problems/0513.找树左下角的值.md +++ b/problems/0513.找树左下角的值.md @@ -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; +} +```

diff --git a/problems/kama54.替换数字.md b/problems/kama54.替换数字.md index d68bbc2c..d5abbc4d 100644 --- a/problems/kama54.替换数字.md +++ b/problems/kama54.替换数字.md @@ -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)) +} +```` diff --git a/problems/kama55.右旋字符串.md b/problems/kama55.右旋字符串.md index f3841930..17c97eaa 100644 --- a/problems/kama55.右旋字符串.md +++ b/problems/kama55.右旋字符串.md @@ -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: diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index 8089af64..ad79424d 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -741,6 +741,99 @@ impl Solution{ } } ``` +### C# +```C# +// 前序遍历 +public IList PreorderTraversal(TreeNode root) +{ + var res = new List(); + var st = new Stack(); + 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 InorderTraversal(TreeNode root) +{ + var res = new List(); + var st = new Stack(); + 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 PostorderTraversal(TreeNode root) +{ + var res = new List(); + var st = new Stack(); + 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; +} +```

diff --git a/problems/二叉树的迭代遍历.md b/problems/二叉树的迭代遍历.md index 69007995..e39709b8 100644 --- a/problems/二叉树的迭代遍历.md +++ b/problems/二叉树的迭代遍历.md @@ -695,6 +695,67 @@ impl Solution { } } ``` +### C# +```C# +// 前序遍历 +public IList PreorderTraversal(TreeNode root) +{ + var st = new Stack(); + var res = new List(); + 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 InorderTraversal(TreeNode root) +{ + var st = new Stack(); + var res = new List(); + 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 PostorderTraversal(TreeNode root) +{ + var res = new List(); + var st = new Stack(); + 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; +} +```

diff --git a/problems/二叉树的递归遍历.md b/problems/二叉树的递归遍历.md index 730b18ac..6dad6e56 100644 --- a/problems/二叉树的递归遍历.md +++ b/problems/二叉树的递归遍历.md @@ -565,6 +565,60 @@ impl Solution { } ``` +### C# +```C# +// 前序遍历 +public IList PreorderTraversal(TreeNode root) +{ + var res = new List(); + if (root == null) return res; + Traversal(root, res); + return res; + +} +public void Traversal(TreeNode cur, IList res) +{ + if (cur == null) return; + res.Add(cur.val); + Traversal(cur.left, res); + Traversal(cur.right, res); +} +``` +```C# +// 中序遍历 +public IList InorderTraversal(TreeNode root) +{ + var res = new List(); + if (root == null) return res; + Traversal(root, res); + return res; +} +public void Traversal(TreeNode cur, IList res) +{ + if (cur == null) return; + Traversal(cur.left, res); + res.Add(cur.val); + Traversal(cur.right, res); +} +``` +```C# +// 后序遍历 +public IList PostorderTraversal(TreeNode root) +{ + var res = new List(); + if (root == null) return res; + Traversal(root, res); + return res; +} +public void Traversal(TreeNode cur, IList res) +{ + if (cur == null) return; + Traversal(cur.left, res); + Traversal(cur.right, res); + res.Add(cur.val); +} +``` +