From b8f1ef6d9543a996bf6ebdb7930c3d6eb75c321a Mon Sep 17 00:00:00 2001 From: XinRuoShi Date: Sun, 5 May 2019 10:03:06 +0800 Subject: [PATCH] week 3/4 push --- Week_03/id_86/LeetCode_104_086.cs | 17 +++++++++++++ Week_03/id_86/LeetCode_429_086.cs | 42 +++++++++++++++++++++++++++++++ Week_04/id_86/LeetCode_169_086.cs | 40 +++++++++++++++++++++++++++++ Week_04/id_86/LeetCode_746_086.cs | 21 ++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 Week_03/id_86/LeetCode_104_086.cs create mode 100644 Week_03/id_86/LeetCode_429_086.cs create mode 100644 Week_04/id_86/LeetCode_169_086.cs create mode 100644 Week_04/id_86/LeetCode_746_086.cs diff --git a/Week_03/id_86/LeetCode_104_086.cs b/Week_03/id_86/LeetCode_104_086.cs new file mode 100644 index 00000000..d22e5773 --- /dev/null +++ b/Week_03/id_86/LeetCode_104_086.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AlgorithmTest +{ + class LeetCode_104_086 + { + public int MaxDepth(TreeNode root) + { + if (root == null) return 0; + return Math.Max(MaxDepth(root.left), MaxDepth(root.right)) + 1; + } + } +} diff --git a/Week_03/id_86/LeetCode_429_086.cs b/Week_03/id_86/LeetCode_429_086.cs new file mode 100644 index 00000000..87129081 --- /dev/null +++ b/Week_03/id_86/LeetCode_429_086.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AlgorithmTest +{ + class LeetCode_429_086 + { + public IList> LevelOrder(Node root) + { + IList> result = new List>(); + + if (root != null) + { + var q = new Queue(); + q.Enqueue(root); + + while (q.Count != 0) + { + int num = q.Count; + IList r = new List(); + + for (int i = 0; i < num; i++) + { + Node n = q.Dequeue(); + r.Add(n.val); + foreach (Node c in n.children) + { + q.Enqueue(c); + } + } + result.Add(r); + } + } + return result; + } + } +} + + diff --git a/Week_04/id_86/LeetCode_169_086.cs b/Week_04/id_86/LeetCode_169_086.cs new file mode 100644 index 00000000..ac9b8e52 --- /dev/null +++ b/Week_04/id_86/LeetCode_169_086.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AlgorithmTest +{ + class LeetCode_169_086 + { + public int MethodFor169_1(int[] num) + { + int n = num[0]; + int count = 1; + + for (int i = 1; i < num.Length; i++) + { + if (count == 0) + { + count++; + n = num[i]; + } + else if (n == num[i]) + { + count++; + } + else count--; + + } + return n; + } + + //直接调用 Array.Sort + public int MethodFor169_2(int[] num) + { + Array.Sort(num); + return num[num.Length / 2]; + } + } +} diff --git a/Week_04/id_86/LeetCode_746_086.cs b/Week_04/id_86/LeetCode_746_086.cs new file mode 100644 index 00000000..31a42328 --- /dev/null +++ b/Week_04/id_86/LeetCode_746_086.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AlgorithmTest +{ + class LeetCode_746_086 + { + public int MethodFor746_1(int[] cost) + { + for (int i = 2; i < cost.Length; i++) + { + cost[i] += Math.Min(cost[i - 2], cost[i - 1]); + } + + return Math.Min(cost[cost.Length - 1], cost[cost.Length - 2]); + } + } +}