diff --git a/DataStructureAndAlgorithm/16 Leetcode同步练习(六).md b/DataStructureAndAlgorithm/16 Leetcode同步练习(六).md new file mode 100644 index 0000000..edd67d6 --- /dev/null +++ b/DataStructureAndAlgorithm/16 Leetcode同步练习(六).md @@ -0,0 +1,1054 @@ + +# Leetcodeͬϰ +## Ŀ¼ +- Ŀ01ͬ +- Ŀ02Գƶ +- Ŀ03 +- Ŀ04 Pow(x, n) +- Ŀ05Ӽ +- Ŀ06ױ +- Ŀ07 +- Ŀ08ǰ +- Ŀ09 +- Ŀ10ĺ + +--- +## Ŀ01ͬ + +> - ţ100 +> - Ѷȣ +> - https://leetcode-cn.com/problems/same-tree/ + +дһǷͬ + +ڽṹͬҽڵֵͬΪͬġ + +**ʾ 1**: + +``` +: 1 1 + / \ / \ + 2 3 2 3 + + [1,2,3], [1,2,3] + +: true +``` + +**ʾ 2**: + +``` +: 1 1 + / \ + 2 2 + + [1,2], [1,null,2] + +: false +``` + +**ʾ 3**: +``` +: 1 1 + / \ / \ + 2 1 1 2 + + [1,2,1], [1,1,2] + +: false +``` + +**ο룺** + +- ִнͨ +- ִʱ160 ms, C# ύл 5.85% û +- ڴģ24 MB, C# ύл 6.67% û + +```c +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int x) { val = x; } + * } + */ + +public class Solution +{ + public bool IsSameTree(TreeNode p, TreeNode q) + { + //ݹֹ + if (p == null && q == null) + return true; + + if (p != null && q != null && p.val == q.val) + { + return IsSameTree(p.left, q.left) + && IsSameTree(p.right, q.right); + } + return false; + } +} +``` + +--- +## Ŀ02Գƶ + +> - ţ101 +> - Ѷȣ +> - https://leetcode-cn.com/problems/symmetric-tree/ + +һǷǾԳƵġ + +磬 `[1,2,2,3,4,4,3]` ǶԳƵġ + +``` + 1 + / \ + 2 2 + / \ / \ +3 4 4 3 +``` + + `[1,2,2,null,3,null,3]` ǾԳƵ: + +``` + 1 + / \ + 2 2 + \ \ + 3 3 +``` + +**ο룺** + +**һ֣õݹķ** + +- ִнͨ +- ִʱ132 ms, C# ύл 16.67% û +- ڴģ25.1 MB, C# ύл 5.17% û + + +```c +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int x) { val = x; } + * } + */ + +//ԳƵĵݹ麯 +public class Solution +{ + public bool IsSymmetric(TreeNode root) + { + return IsMirror(root, root); + } + private bool IsMirror(TreeNode t1, TreeNode t2) + { + if (t1 == null && t2 == null) return true; + if (t1 == null || t2 == null) return false; + return (t1.val == t2.val) + && IsMirror(t1.left, t2.right) + && IsMirror(t1.right, t2.left); + } +} +``` + +**ڶ֣öеķ** + +**˼·**öIJαķʽʵ֡ + +- ִнͨ +- ִʱ112 ms, C# ύл 70.93% û +- ڴģ24.9 MB, C# ύл 5.17% û + +```c +public class Solution +{ + public bool IsSymmetric(TreeNode root) + { + if (root == null) + return true; + + Queue nodes = new Queue(); + nodes.Enqueue(root.left); + nodes.Enqueue(root.right); + while (nodes.Count != 0) + { + TreeNode node1 = nodes.Dequeue(); + TreeNode node2 = nodes.Dequeue(); + + if (node1 == null && node2 == null) + continue; + if (node1 == null || node2 == null) + return false; + if (node1.val != node2.val) + return false; + nodes.Enqueue(node1.left); + nodes.Enqueue(node2.right); + nodes.Enqueue(node1.right); + nodes.Enqueue(node2.left); + } + return true; + } +} +``` + +--- +## Ŀ03 + +> - ţ104 +> - Ѷȣ +> - https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ + + +һҳȡ + +Ϊڵ㵽ԶҶӽڵ·ϵĽڵ + +˵: ҶӽڵָûӽڵĽڵ㡣 + +**ʾ** + + [3,9,20,null,null,15,7] + +```c + 3 + / \ + 9 20 + / \ + 15 7 +``` + + 3 + +**ο룺** + +**һ֣öʵֲα˼·** + +- ִнͨ +- ִʱ108 ms, C# ύл 88.13% û +- ڴģ25.5 MB, C# ύл 5.97% û + +```c +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int x) { val = x; } + * } + */ +public class Solution +{ + public int MaxDepth(TreeNode root) + { + if (root == null) + return 0; + + Queue q = new Queue(); + int deep = 0; + q.Enqueue(root); + + while (q.Count != 0) + { + deep++; + int count = 0; + int size = q.Count; + + while (count < size) + { + TreeNode node = q.Dequeue(); + count++; + + if (node.left != null) + q.Enqueue(node.left); + if (node.right != null) + q.Enqueue(node.right); + } + } + return deep; + } +} +``` + + + +**ڶ֣õݹ** + +**˼·**ݹֱȣӵԭвϣ󷵻еֵ + +- ִнͨ +- ִʱ132 ms, C# ύл 16.62% û +- ڴģ25.5 MB, C# ύл 6.06% û + +```c +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int x) { val = x; } + * } + */ +public class Solution +{ + public int MaxDepth(TreeNode root) + { + if (root == null) + return 0; + int llen = 1; + int rlen = 1; + if (root.left != null) + { + llen += MaxDepth(root.left); + } + if (root.right != null) + { + rlen += MaxDepth(root.right); + } + return llen > rlen ? llen : rlen; + } +} +``` +--- +## Ŀ04 Pow(x, n) + +> - ţ50 +> - Ѷȣе +> - https://leetcode-cn.com/problems/powx-n/ + + +ʵ `pow(x, n)` x n ݺ + +**ʾ 1:** + +```c +: 2.00000, 10 +: 1024.00000 +``` + +**ʾ 2:** +```c +: 2.10000, 3 +: 9.26100 +``` + +**ʾ 3:** +```c +: 2.00000, -2 +: 0.25000 +: 2-2 = 1/22 = 1/4 = 0.25 +``` + +**ʾ 4:** +```c +: 1.00000, -2147483648 +: 1.00000 +``` + +**˵:** + +- -100.0 < x < 100.0 +- n 32 λзֵΧ [?2^31, 2^31 ? 1] + + +**˼·**ÿݷ + +Ҫ`a^b`ô`b`ԲɶƱʾ統`b = 5`ʱ5Ķ0101`5 = 2^30 + 2^21 + 2^10 + 2^01`ˣǽ`a^5`תΪ `a^(2^30 + 2^21 + 2^10 + 2^01)``a^(2^0) a^(2^2)` + + +![](https://img-blog.csdnimg.cn/20200419235913493.png) + +2ݣȻx2ݴηٰnɶƣѶƵжӦλ1ֵ͵õ˽׷Ϊ **ݷ** + + +**ο** + +- ִнͨ +- ִʱ56 ms, C# ύл 51.87% û +- ڴģ15.1 MB, C# ύл 50.00% û + +```c +public class Solution +{ + public double MyPow(double x, int n) + { + int neg = n < 0 ? -1 : 1; + long g = Math.Abs((long)n); + + double[] d = new double[32]; + d[0] = x; + for (int i = 1; i < 32; i++) + { + d[i] = d[i - 1] * d[i - 1]; + } + + double result = 1.0d; + for (int i = 0; i < 32; i++) + { + int mask = 1 << i; + if ((mask & g) != 0) + { + result *= d[i]; + } + } + return neg != -1 ? result : 1.0 / result; + } +} +``` +--- +## Ŀ05Ӽ + +> - ţ78 +> - Ѷȣе +> - https://leetcode-cn.com/problems/subsets/ + + +һ**ظԪ** numsظпܵӼݼ + +˵⼯ܰظӼ + +ʾ: +```c +: nums = [1,2,3] +: +[ + [3], + [1], + [2], + [1,2,3], + [1,3], + [2,3], + [1,2], + [] +] +``` + +**ο** + +**һ֣ݷ** + +nums[i]ΪʼҺֵҪǰһֵظ + +![ݹ](https://img-blog.csdnimg.cn/20190913152245222.png) + +- ״̬ͨ +- ʱ: 356 ms, C# ύл 92.31% û +- ڴ: 29.2 MB, C# ύл 6.67% û + +```c +public class Solution +{ + private IList> _result; + + public IList> Subsets(int[] nums) + { + _result = new List>(); + int len = nums.Length; + + if (len == 0) + { + return _result; + } + IList item = new List(); + Find(nums, 0, item); + return _result; + } + + private void Find(int[] nums, int begin, IList item) + { + // ע⣺Ҫ new һ + _result.Add(new List(item)); + + if (begin == nums.Length) + return; + + for (int i = begin; i < nums.Length; i++) + { + item.Add(nums[i]); + Find(nums, i + 1, item); + + // ⣬״̬ڵݹɺҪ + item.RemoveAt(item.Count - 1); + } + } +} +``` + +**ڶ֣Ӽչ** + +- ״̬ͨ +- ִʱ: 352 ms, C# ύл 94.51% û +- ڴ: 29.2 MB, C# ύл 6.67% û + +```c +public class Solution +{ + public IList> Subsets(int[] nums) + { + IList> result = new List>(); + IList item = new List(); + result.Add(item); + for (int i = 0; i < nums.Length; i++) + { + for (int j = 0, len = result.Count; j < len; j++) + { + item = new List(result[j]); + item.Add(nums[i]); + result.Add(item); + } + } + return result; + } +} +``` + +**֣λ** + +**˼·** ϵ˼· + +`{1,2,3}`Ϊ`2^3`Ӽ + +```c +000 -> [] +100 -> [1] +101 -> [1,3] +110 -> [1,2] +111 -> [1,2,3] +... +``` + +- ״̬ͨ +- ִʱ: 348 ms, C# ύл 97.80% û +- ڴ: 29.5 MB, C# ύл 6.67% û + +```c +public class Solution +{ + public IList> Subsets(int[] nums) + { + IList> result = new List>(); + int count = nums.Length; + + for (int i = 0; i < 1 << count; i++) + { + IList item = new List(); + for (int j = 0; j < count; j++) + { + int mask = 1 << j; + if ((mask & i) != 0) + item.Add(nums[j]); + } + result.Add(item); + } + return result; + } +} +``` + +--- +## Ŀ06ױ + +> - ţ89 +> - Ѷȣе +> - https://leetcode-cn.com/problems/gray-code/ + +ױһϵͳڸϵͳУֵһλIJ졣 + +һλķǸ nӡױСױб 0 ͷ + +ʾ 1: +```c +: 2 +: [0,1,3,2] +: +00 - 0 +01 - 1 +11 - 3 +10 - 2 + +ڸ nױвΨһ +磬[0,2,3,1] ҲһЧĸױС + +00 - 0 +10 - 2 +11 - 3 +01 - 1 +``` + +ʾ 2: +```c +: 0 +: [0] +: Ƕױб 0 ͷ + λΪ n ĸױУ䳤Ϊ 2^n + n = 0 ʱΪ 2^0 = 1 + ˣ n = 0 ʱױΪ [0] +``` + +**˼·** + + +![׸](https://img-blog.csdnimg.cn/20190915115931873.png) + + n λƵ n+1 λʱn+1 λ n λͬʱ n λڸλһλ 1 γɵһһҪǰһС + +**ο** + +- ״̬ͨ +- 12 / 12 ͨ +- ִʱ: 296 ms, C# ύл 95.83% û +- ڴ: 24.8 MB, C# ύл 16.67% û + +```c +public class Solution +{ + public IList GrayCode(int n) + { + IList lst = new List(); + lst.Add(0); + for (int i = 1; i <= n; i++) + { + for (int j = lst.Count - 1; j >= 0; j--) + { + int item = lst[j] + (1 << i - 1); + lst.Add(item); + } + } + return lst; + } +} +``` + + +--- +## Ŀ07 + + + +> - ţ236 +> - Ѷȣе +> - https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/ + +һ, ҵָڵȡ + +ٶȰٿȵĶΪи T pqȱʾΪһ x x pq x ȾܴһڵҲԼ + +磬¶: root = [3,5,1,6,2,0,8,null,null,7,4] + +![](https://img-blog.csdnimg.cn/20190922095031349.png) + +ʾ 1: +```c +: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 +: 3 +: ڵ 5 ͽڵ 1 ǽڵ 3 +``` + +ʾ 2: +```c +: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 +: 5 +: ڵ 5 ͽڵ 4 ǽڵ 5ΪݶȽڵΪڵ㱾 +``` + +˵: + +- нڵֵΨһġ +- pq ΪͬڵҾڸĶС + + +**ο** + +**˼·**õݹ + +- ״̬ͨ +- ִʱ: 132 ms, C# ύл 96.10% û +- ڴ: 27.5 MB, C# ύл 20.00% û + +```c +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int x) { val = x; } + * } + */ + +public class Solution +{ + public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) + { + return Find(root, p, q); + } + + private TreeNode Find(TreeNode current, TreeNode p, TreeNode q) + { + if (current == null || current == p || current == q) + return current; + TreeNode left = Find(current.left, p, q); + TreeNode right = Find(current.right, p, q); + if (left != null && right != null) + return current; + return left != null ? left : right; + } +} +``` + + + +--- +## Ŀ08ǰ + +> - ţ144 +> - Ѷȣе +> - https://leetcode-cn.com/problems/binary-tree-preorder-traversal/ + + +һ ǰ + +**ʾ:** + + +```c +: [1,null,2,3] + 1 + \ + 2 + / + 3 + +: [1,2,3] +``` + + +: ݹ㷨ܼ򵥣ͨ㷨 + + +**ο** + +**һ֣ջ** + +- ִнͨ +- ִʱ276 ms, C# ύл 84.15% û +- ڴģ29.9 MB, C# ύл 5.00% û + +```c +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int x) { val = x; } + * } + */ +public class Solution +{ + public IList PreorderTraversal(TreeNode root) + { + IList lst = new List(); + Stack stack = new Stack(); + while (stack.Count != 0 || root != null) + { + if (root != null) + { + stack.Push(root); + lst.Insert(lst.Count, root.val); + root = root.left; + } + else + { + TreeNode node = stack.Pop(); + root = node.right; + } + } + return lst; + } +} +``` + +**ڶ֣õݹ** + +- ִнͨ +- ִʱ280 ms, C# ύл 79.27% û +- ڴģ29.8 MB, C# ύл 5.00% û + +```c +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int x) { val = x; } + * } + */ +public class Solution +{ + public IList PreorderTraversal(TreeNode root) + { + IList lst = new List(); + PreOrder(root, lst); + return lst; + } + private void PreOrder(TreeNode node, IList lst) + { + if (node == null) + return; + + lst.Add(node.val); + PreOrder(node.left, lst); + PreOrder(node.right, lst); + } +} +``` + + +--- +## Ŀ09 + +> - ţ94 +> - Ѷȣе +> - https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ + +һ + +**ʾ:** + +```c +: [1,null,2,3] + 1 + \ + 2 + / + 3 + +: [1,3,2] +``` + +: ݹ㷨ܼ򵥣ͨ㷨 + + +**ο룺** + +**һ֣ջ** + +- ִнͨ +- ִʱ284 ms, C# ύл 53.59% û +- ڴģ30 MB, C# ύл 6.67% û + +```c +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int x) { val = x; } + * } + */ +public class Solution +{ + public IList InorderTraversal(TreeNode root) + { + IList lst = new List(); + Stack stack = new Stack(); + while (stack.Count != 0 || root != null) + { + if (root != null) + { + stack.Push(root); + root = root.left; + } + else + { + TreeNode node = stack.Pop(); + lst.Add(node.val); + root = node.right; + } + } + return lst; + } +} +``` + + + +**ڶ֣ʹõݹ** + +- ִнͨ +- ִʱ264 ms, C# ύл 99.16% û +- ڴģ29.8 MB, C# ύл 6.67% û + +```c +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int x) { val = x; } + * } + */ +public class Solution +{ + public IList InorderTraversal(TreeNode root) + { + IList lst = new List(); + MidOrder(root, lst); + return lst; + } + private void MidOrder(TreeNode node, IList lst) + { + if (node == null) + return; + MidOrder(node.left, lst); + lst.Add(node.val); + MidOrder(node.right, lst); + } +} +``` + + + +--- +## Ŀ10ĺ + +> - ţ145 +> - Ѷȣ +> - https://leetcode-cn.com/problems/binary-tree-postorder-traversal/ + + +һ + +**ʾ:** + + +```c +: [1,null,2,3] + 1 + \ + 2 + / + 3 + +: [3,2,1] +``` + + +: ݹ㷨ܼ򵥣ͨ㷨 + + +**ο** + + +**һ֣ջ** + +ǰ˳Ϊ -> -> + +˳Ϊ -> -> + +1ǽǰнڵβ߼޸Ϊڵͷ + +ôͱΪˣ -> -> + +2ǽ˳ɴ޸Ϊҵ1 + +ôͱΪˣ -> -> + +պǺ˳ + +˼·Ǵʽ£ + +һ޸ǰУڵдĴ룬β޸Ϊ + +ڶ޸ǰУÿȲ鿴ڵٲ鿴ҽڵ߼ΪȲ鿴ҽڵٲ鿴ڵ + +- ִнͨ +- ִʱ272 ms, C# ύл 98.15% û +- ڴģ30 MB, C# ύл 6.90% û + +```c +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int x) { val = x; } + * } + */ +public class Solution +{ + public IList PostorderTraversal(TreeNode root) + { + IList lst = new List(); + Stack stack = new Stack(); + while (stack.Count != 0 || root != null) + { + if (root != null) + { + stack.Push(root); + lst.Insert(0, root.val); + root = root.right; + } + else + { + TreeNode node = stack.Pop(); + root = node.left; + } + } + return lst; + } +} +``` + +**ڶ֣õݹ** + +- ִнͨ +- ִʱ276 ms, C# ύл 89.81% û +- ڴģ30 MB, C# ύл 6.90% û + +```c +/** + * Definition for a binary tree node. + * public class TreeNode { + * public int val; + * public TreeNode left; + * public TreeNode right; + * public TreeNode(int x) { val = x; } + * } + */ +public class Solution +{ + public IList PostorderTraversal(TreeNode root) + { + IList lst = new List(); + PostOrder(root, lst); + return lst; + } + private void PostOrder(TreeNode node, IList lst) + { + if (node == null) + return; + + PostOrder(node.left, lst); + PostOrder(node.right, lst); + lst.Add(node.val); + } +} +``` + +