From 09835e74442c5dfdd118e13ffc43c06da5e419d7 Mon Sep 17 00:00:00 2001 From: to_Geek Date: Fri, 10 Nov 2023 22:00:29 +0800 Subject: [PATCH 01/23] =?UTF-8?q?=F0=9F=93=9D=20Update=200383.=E8=B5=8E?= =?UTF-8?q?=E9=87=91=E4=BF=A1.md=20with=20python3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0383.赎金信.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 From 8945fd98094efe154431cb51a7edf01e3b50af60 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sat, 11 Nov 2023 09:12:17 +0800 Subject: [PATCH 02/23] =?UTF-8?q?Update.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E9=80=92=E5=BD=92=E9=81=8D=E5=8E=86=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的递归遍历.md | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) 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); +} +``` +

From 5d9d37f01d59b2fa95001018fb24e4b74e2a04f9 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sun, 12 Nov 2023 09:15:59 +0800 Subject: [PATCH 03/23] =?UTF-8?q?Update.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E8=BF=AD=E4=BB=A3=E9=81=8D=E5=8E=86=EF=BC=8CC#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的迭代遍历.md | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) 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; +} +```

From 8e463e0b64b02656c41202df52efe249319034e7 Mon Sep 17 00:00:00 2001 From: yihug <31957705+DanielChungYi@users.noreply.github.com> Date: Sun, 12 Nov 2023 12:15:44 +0900 Subject: [PATCH 04/23] =?UTF-8?q?[typo=20fix]:=20Update=20kama54.=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kama54.替换数字.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/kama54.替换数字.md b/problems/kama54.替换数字.md index 80c87951..6147f2d1 100644 --- a/problems/kama54.替换数字.md +++ b/problems/kama54.替换数字.md @@ -46,7 +46,7 @@ 从前向后填充就是O(n^2)的算法了,因为每次添加元素都要将添加元素之后的所有元素整体向后移动。 -**其实很多数组填充类的问题,其做饭都是先预先给数组扩容带填充后的大小,然后在从后向前进行操作。** +**其实很多数组填充类的问题,其做法都是先预先给数组扩容带填充后的大小,然后在从后向前进行操作。** 这么做有两个好处: From ce2ffd073c20fa72c07df54b4566d285172276c4 Mon Sep 17 00:00:00 2001 From: to_Geek Date: Sun, 12 Nov 2023 23:44:17 +0800 Subject: [PATCH 05/23] =?UTF-8?q?=F0=9F=93=9D=20Update=200344.=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2.md=20with=20python3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0344.反转字符串.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 From 436bd5a1a25d317824af0405bba0d023a7619a16 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Mon, 13 Nov 2023 09:21:04 +0800 Subject: [PATCH 06/23] =?UTF-8?q?Update.=E7=BB=9F=E4=B8=80=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E6=B3=95=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的统一迭代法.md | 93 +++++++++++++++++++++++++ 1 file changed, 93 insertions(+) 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; +} +```

From aad5029a1cb33461905df368c951bceeb2fbc797 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 14 Nov 2023 09:11:17 +0800 Subject: [PATCH 07/23] =?UTF-8?q?Update=20102.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) 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.二叉树的右视图 From bc988fb846ab845e61a748aad6e9ec79f0ab2339 Mon Sep 17 00:00:00 2001 From: changknn Date: Tue, 14 Nov 2023 09:58:16 +0800 Subject: [PATCH 08/23] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c1296ed3..143a8ba2 100644 --- a/README.md +++ b/README.md @@ -397,7 +397,7 @@ * [图论:417.太平洋大西洋水流问题](./problems/0417.太平洋大西洋水流问题.md) * [图论:827.最大人工岛](./problems/0827.最大人工岛.md) * [图论:127. 单词接龙](./problems/0127.单词接龙.md) -* [图论:841.钥匙和房间](./problems/841.钥匙和房间) +* [图论:841.钥匙和房间](./problems/0841.钥匙和房间.md) * [图论:463. 岛屿的周长](./problems/0463.岛屿的周长.md) * [图论:并查集理论基础](./problems/) * [图论:1971. 寻找图中是否存在路径](./problems/1971.寻找图中是否存在路径.md) From 08a77b9e060985cbd25205144b8e86d57d8b61d4 Mon Sep 17 00:00:00 2001 From: changknn Date: Tue, 14 Nov 2023 11:00:01 +0800 Subject: [PATCH 09/23] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 143a8ba2..15b10bfa 100644 --- a/README.md +++ b/README.md @@ -399,7 +399,7 @@ * [图论:127. 单词接龙](./problems/0127.单词接龙.md) * [图论: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) From 103256a36a20fa48514f3d6cb2516fecda681c68 Mon Sep 17 00:00:00 2001 From: xin Date: Wed, 15 Nov 2023 08:34:47 +0800 Subject: [PATCH 10/23] =?UTF-8?q?update:=20=E6=9B=B4=E6=96=B00104.?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1?= =?UTF-8?q?=E5=BA=A6=E9=87=8C=E7=9A=84=20n=20=E5=8F=89=E6=A0=91=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E6=B7=B1=E5=BA=A6=E7=9A=84=20ts=20=E8=A7=A3=E6=B3=95?= =?UTF-8?q?=EF=BC=8C=E5=8E=9F=E6=9C=AC=E4=BB=A3=E7=A0=81=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E9=80=9A=E8=BF=87=20leetcode=20=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0104.二叉树的最大深度.md | 42 ++++++++++++++--------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 05044375..b115762e 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 +} ``` From dba4e9d00ec28ea484715bd5fa341ae8aed9ed98 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Wed, 15 Nov 2023 08:57:19 +0800 Subject: [PATCH 11/23] =?UTF-8?q?Update226.=E7=BF=BB=E8=BD=AC=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=EF=BC=8C=E7=AE=80=E5=8C=96C#=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0226.翻转二叉树.md | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) 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; +} } ``` From b51573efa69a3e4fead79bbcba62c77f7ecedb09 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Wed, 15 Nov 2023 09:16:04 +0800 Subject: [PATCH 12/23] =?UTF-8?q?Update101.=E5=AF=B9=E7=A7=B0=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0101.对称二叉树.md | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) 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; +} +```

From 3ffca338cafa5ba72d81048d762211488f7d19f7 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Thu, 16 Nov 2023 09:12:06 +0800 Subject: [PATCH 13/23] =?UTF-8?q?Update104.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0104.二叉树的最大深度.md | 55 +++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 05044375..ab3ce52e 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -1022,6 +1022,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; +} +```

From bb66452ece363f599b89761269e7311a4654404a Mon Sep 17 00:00:00 2001 From: gdstzmy <79707886+gdstzmy@users.noreply.github.com> Date: Thu, 16 Nov 2023 15:04:46 +0800 Subject: [PATCH 14/23] =?UTF-8?q?Update=20kama54.=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E6=95=B0=E5=AD=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kama54.替换数字.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/problems/kama54.替换数字.md b/problems/kama54.替换数字.md index d68bbc2c..a8c55792 100644 --- a/problems/kama54.替换数字.md +++ b/problems/kama54.替换数字.md @@ -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)) +} +```` From 972ca2de45f2a85baa055122944cb9c6aa4bfdcc Mon Sep 17 00:00:00 2001 From: gdstzmy <79707886+gdstzmy@users.noreply.github.com> Date: Thu, 16 Nov 2023 16:46:36 +0800 Subject: [PATCH 15/23] =?UTF-8?q?Update=20kama55.=E5=8F=B3=E6=97=8B?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kama55.右旋字符串.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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: From cfc78c3c57d5d9a8c79df710c36979700fd43064 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Fri, 17 Nov 2023 19:33:16 +0800 Subject: [PATCH 16/23] =?UTF-8?q?Update111.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0111.二叉树的最小深度.md | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) 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; +} +```

From 0843f2282dbcc9597bf156870149632d80def198 Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sat, 18 Nov 2023 09:48:26 +0800 Subject: [PATCH 17/23] =?UTF-8?q?Update=20222.=E5=AE=8C=E5=85=A8=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9=E4=B8=AA=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E9=80=92=E5=BD=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0222.完全二叉树的节点个数.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) 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; + +} +```

From eb0504e5305ae37f3f6f569ebde423206ea11b0f Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Sun, 19 Nov 2023 08:49:19 +0800 Subject: [PATCH 18/23] =?UTF-8?q?Update=20110.=E5=B9=B3=E8=A1=A1=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0110.平衡二叉树.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) 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; +} +```

From a9923f809d7f22b5271a09764788aa6f16fa430e Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Mon, 20 Nov 2023 09:48:41 +0800 Subject: [PATCH 19/23] =?UTF-8?q?Update=200257.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=89=80=E6=9C=89=E8=B7=AF=E5=BE=84=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0C#=E9=80=92=E5=BD=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0257.二叉树的所有路径.md | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) 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); + } +} +```

From 34de1fd8b0fde25caef3626bf6a848f080607aed Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Tue, 21 Nov 2023 09:07:56 +0800 Subject: [PATCH 20/23] =?UTF-8?q?Update=20404.=E5=B7=A6=E5=8F=B6=E5=AD=90?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E9=80=92?= =?UTF-8?q?=E5=BD=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0404.左叶子之和.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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; + +} +```

From b1d56c8cffe8fb25fc9bbf7d8f5aa768491bb68d Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Wed, 22 Nov 2023 10:02:57 +0800 Subject: [PATCH 21/23] =?UTF-8?q?Update=200513.=E6=89=BE=E6=A0=91=E5=B7=A6?= =?UTF-8?q?=E4=B8=8B=E6=96=B9=E7=9A=84=E5=80=BC=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?C#=E9=80=92=E5=BD=92=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0513.找树左下角的值.md | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) 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; +} +```

From 14906d78cb92a8bbfd034634c80daf1a5fa355f0 Mon Sep 17 00:00:00 2001 From: han854 <1229369094@qq.com> Date: Wed, 22 Nov 2023 16:08:00 +0800 Subject: [PATCH 22/23] =?UTF-8?q?=E4=BF=AE=E6=AD=A30070.=E7=88=AC=E6=A5=BC?= =?UTF-8?q?=E6=A2=AF=E5=AE=8C=E5=85=A8=E8=83=8C=E5=8C=85=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0070.爬楼梯完全背包版本.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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]; } } From 0d993075669743879b59c8a549783604dbbb7b2f Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Thu, 23 Nov 2023 09:38:13 +0800 Subject: [PATCH 23/23] =?UTF-8?q?Update0112.=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8C=EF=BC=8C=E6=B7=BB=E5=8A=A0C#=E9=80=92=E5=BD=92?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 11 +++++++++++ 1 file changed, 11 insertions(+) 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); +} ```