From 7c7d9c415f6d0568286bab54aa2fb8d3af32e4ba Mon Sep 17 00:00:00 2001 From: Yrp1994 <2927848012@qq.com> Date: Fri, 28 Jun 2019 20:55:26 +0800 Subject: [PATCH 1/7] =?UTF-8?q?720.=20=E8=AF=8D=E5=85=B8=E4=B8=AD=E6=9C=80?= =?UTF-8?q?=E9=95=BF=E7=9A=84=E5=8D=95=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_7/LeetCode_200_720.cs | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Week_04/id_7/LeetCode_200_720.cs diff --git a/Week_04/id_7/LeetCode_200_720.cs b/Week_04/id_7/LeetCode_200_720.cs new file mode 100644 index 00000000..fd4fc224 --- /dev/null +++ b/Week_04/id_7/LeetCode_200_720.cs @@ -0,0 +1,44 @@ +/* + * @lc app=leetcode.cn id=720 lang=csharp + * + * [720] 词典中最长的单词 + */ +using System; +using System.Collections.Generic; + +public class Solution { + // 先排序,后将所有单词放入集合中,遍历每一个单词, + // 然后对于每个单词进行每次去除末尾一个字母的循环操作, + // 如果可以执行到单词长度为0,则与strRes进行比较,比strRes长,则替换strRes + public string LongestWord (string[] words) { + if (words.Length == 1) { + return words[0]; + } + + Array.Sort (words); + string strRes = words[0]; + + HashSet hsWord = new HashSet (); + foreach (var word in words) { + hsWord.Add (word); + } + + foreach (var word in words) { + if (word.Length <= 1 || word.Length <= strRes.Length) { + continue; + } + string temp = word; + while (temp.Length > 0) { + if (!hsWord.Contains (temp)) { + break; + } + temp = temp.Substring (0, temp.Length - 1); + if (temp.Length == 0 && strRes.Length < word.Length) { + strRes = word; + } + } + } + + return strRes; + } +} \ No newline at end of file From 95765a0f7db112699a1fe3de920753e4f57449a6 Mon Sep 17 00:00:00 2001 From: Yrp1994 <2927848012@qq.com> Date: Sun, 30 Jun 2019 00:09:10 +0800 Subject: [PATCH 2/7] =?UTF-8?q?[720]=20=E8=AF=8D=E5=85=B8=E4=B8=AD?= =?UTF-8?q?=E6=9C=80=E9=95=BF=E7=9A=84=E5=8D=95=E8=AF=8D=20=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AD=97=E5=85=B8=E6=A0=91Trie=E8=A7=A3?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_7/LeetCode_200_720.cs | 114 +++++++++++++++++++++++++------ 1 file changed, 95 insertions(+), 19 deletions(-) diff --git a/Week_04/id_7/LeetCode_200_720.cs b/Week_04/id_7/LeetCode_200_720.cs index fd4fc224..a427a67d 100644 --- a/Week_04/id_7/LeetCode_200_720.cs +++ b/Week_04/id_7/LeetCode_200_720.cs @@ -7,38 +7,114 @@ using System.Collections.Generic; public class Solution { + // 方法1 // 先排序,后将所有单词放入集合中,遍历每一个单词, // 然后对于每个单词进行每次去除末尾一个字母的循环操作, // 如果可以执行到单词长度为0,则与strRes进行比较,比strRes长,则替换strRes - public string LongestWord (string[] words) { - if (words.Length == 1) { - return words[0]; + // public string LongestWord (string[] words) { + // if (words.Length == 1) { + // return words[0]; + // } + + // Array.Sort (words); + // string strRes = words[0]; + + // HashSet hsWord = new HashSet (); + // foreach (var word in words) { + // hsWord.Add (word); + // } + + // foreach (var word in words) { + // if (word.Length <= 1 || word.Length <= strRes.Length) { + // continue; + // } + // string temp = word; + // while (temp.Length > 0) { + // if (!hsWord.Contains (temp)) { + // break; + // } + // temp = temp.Substring (0, temp.Length - 1); + // if (temp.Length == 0 && strRes.Length < word.Length) { + // strRes = word; + // } + // } + // } + + // return strRes; + // } + + // 方法2 + // 构建字典树(Trie),再使用深度优先遍历(dfs)遍历所有节点,找到最长单词 + // 注意点:需要存储字典树中每个节点对应的单词的位置,即代码中end,这样在深度优先比例时,可以直接从数组中取得对应单词 + // dfs,当前代码使用的是非递归,也可以使用递归 + // 此代码从国际版的官方解法中获得 + class Node { + char c; + public Dictionary children = new Dictionary (); + public int end; + public Node (char c) { + this.c = c; } + } - Array.Sort (words); - string strRes = words[0]; + class Trie { + Node root; + public string[] words; - HashSet hsWord = new HashSet (); - foreach (var word in words) { - hsWord.Add (word); + public Trie () { + root = new Node ('0'); } - foreach (var word in words) { - if (word.Length <= 1 || word.Length <= strRes.Length) { - continue; + public void insert (string word, int index) { + Node cur = root; + for (int i = 0; i < word.Length; i++) { + if (cur.children.ContainsKey (word[i])) { + if (cur.children[word[i]] == null) { + cur.children[word[i]] = new Node (word[i]); + } + } else { + cur.children.Add (word[i], new Node (word[i])); + } + + cur = cur.children[word[i]]; } - string temp = word; - while (temp.Length > 0) { - if (!hsWord.Contains (temp)) { - break; + + cur.end = index; + } + + public string dfs () { + string ans = ""; + Stack sk = new Stack (); + sk.Push (root); + while (sk.Count > 0) { + Node node = sk.Pop (); + if (node.end < 1 && node != root) { + continue; } - temp = temp.Substring (0, temp.Length - 1); - if (temp.Length == 0 && strRes.Length < word.Length) { - strRes = word; + if (node != root) { + string word = words[node.end - 1]; + if (word.Length > ans.Length || word.Length == ans.Length && word.CompareTo (ans) < 0) { + ans = word; + } + } + + foreach (var v in node.children.Values) { + sk.Push (v); } } + + return ans; + } + } + + public string LongestWord (string[] words) { + Trie trie = new Trie (); + int index = 0; + foreach (var word in words) { + trie.insert (word, ++index); } + trie.words = words; - return strRes; + return trie.dfs (); } } \ No newline at end of file From fa7f4134948117da9adbc8678ad5025b6bb09032 Mon Sep 17 00:00:00 2001 From: Yrp1994 <2927848012@qq.com> Date: Sun, 30 Jun 2019 00:46:18 +0800 Subject: [PATCH 3/7] =?UTF-8?q?[169]=20=E6=B1=82=E4=BC=97=E6=95=B0=20?= =?UTF-8?q?=E8=A7=A3=E6=B3=951=20=E6=9A=B4=E5=8A=9B=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_7/LeetCode_200_169.cs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Week_04/id_7/LeetCode_200_169.cs diff --git a/Week_04/id_7/LeetCode_200_169.cs b/Week_04/id_7/LeetCode_200_169.cs new file mode 100644 index 00000000..e2100150 --- /dev/null +++ b/Week_04/id_7/LeetCode_200_169.cs @@ -0,0 +1,30 @@ +/* + * @lc app=leetcode.cn id=169 lang=csharp + * + * [169] 求众数 + */ +using System.Collections.Generic; + +public class Solution { + // 解法1 + // 暴力法,直接双层循环遍历所有数字,统计出现次数,大于nums.Length/2就直接返回 + // 优化点:记录之前统计过的数字,再次遇到,则不再统计 + public int MajorityElement (int[] nums) { + HashSet hs = new HashSet (); + for (int i = 0; i < nums.Length; i++) { + if (hs.Contains (nums[i])) { + continue; + } + int count = 0; + for (int j = 0; j < nums.Length; j++) { + if (nums[i] == nums[j]) { count++; } + } + if (count > nums.Length / 2) { + return nums[i]; + } + hs.Add (nums[i]); + } + + return 0; + } +} \ No newline at end of file From 9b43168837b470d24be1fff164c19b2b865696fa Mon Sep 17 00:00:00 2001 From: Yrp1994 <2927848012@qq.com> Date: Sun, 30 Jun 2019 11:10:20 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=85=B6=E4=BB=96?= =?UTF-8?q?=E9=9B=86=E4=B8=AD=E8=A7=A3=E6=B3=95=EF=BC=8C=E5=BD=93=E7=84=B6?= =?UTF-8?q?=E9=87=8D=E7=82=B9=E6=98=AF=E4=BA=8C=E5=88=86=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_7/LeetCode_200_169.cs | 117 +++++++++++++++++++++++++++---- 1 file changed, 103 insertions(+), 14 deletions(-) diff --git a/Week_04/id_7/LeetCode_200_169.cs b/Week_04/id_7/LeetCode_200_169.cs index e2100150..a5e89e50 100644 --- a/Week_04/id_7/LeetCode_200_169.cs +++ b/Week_04/id_7/LeetCode_200_169.cs @@ -3,28 +3,117 @@ * * [169] 求众数 */ +using System; using System.Collections.Generic; public class Solution { // 解法1 // 暴力法,直接双层循环遍历所有数字,统计出现次数,大于nums.Length/2就直接返回 // 优化点:记录之前统计过的数字,再次遇到,则不再统计 + // public int MajorityElement (int[] nums) { + // HashSet hs = new HashSet (); + // for (int i = 0; i < nums.Length; i++) { + // if (hs.Contains (nums[i])) { + // continue; + // } + // int count = 0; + // for (int j = 0; j < nums.Length; j++) { + // if (nums[i] == nums[j]) { count++; } + // } + // if (count > nums.Length / 2) { + // return nums[i]; + // } + // hs.Add (nums[i]); + // } + + // return 0; + // } + + // 解法2,肯定不是自己想出来的,看答案知道的 + // public int MajorityElement (int[] nums) { + // // 摩尔投票法 + // int zongshu = nums[0]; + // int count = 1; + + // for (int i = 1; i < nums.Length; i++) { + // if (zongshu == nums[i]) { + // count++; + // if (count > (nums.Length / 2)) { + // return zongshu; + // } + // } else { + // count--; + // } + + // if (count == 0) { + // zongshu = nums[i]; + // count = 1; + // } + // } + + // return zongshu; + // } + + // 解法3:哈希表,先简单遍历一下,记录所有数出现的次数,然后再遍历哈希表,找出答案 + // public int MajorityElement (int[] nums) { + // Dictionary dicCount = new Dictionary (); + // for (int i = 0; i < nums.Length; i++) { + // if (dicCount.ContainsKey (nums[i])) { + // dicCount[nums[i]]++; + // } else { + // dicCount.Add (nums[i], 1); + // } + // } + + // foreach (var item in dicCount) { + // if (item.Value > nums.Length / 2) { + // return item.Key; + // } + // } + + // return 0; + // } + + // 解法4:排序,方法肯定也不是我想出来的,使用标准函数进行排序,然后直接返回中间数,即为答案 + // public int MajorityElement (int[] nums) { + // Array.Sort (nums); + // return nums[nums.Length / 2]; + // } + + // 解法5 看答案,还有一个随机法,这个哪能想的出来,这么奇葩的方法,这里就不写了 + + // 方法6:分治法,这个才是今天的重点 public int MajorityElement (int[] nums) { - HashSet hs = new HashSet (); - for (int i = 0; i < nums.Length; i++) { - if (hs.Contains (nums[i])) { - continue; - } - int count = 0; - for (int j = 0; j < nums.Length; j++) { - if (nums[i] == nums[j]) { count++; } - } - if (count > nums.Length / 2) { - return nums[i]; - } - hs.Add (nums[i]); + return DivideConquer (nums, 0, nums.Length - 1); + } + + public int DivideConquer (int[] nums, int left, int right) { + if (left == right) { + return nums[left]; + } + + int mid = (right - left) / 2 + left; + int leftRes = DivideConquer (nums, left, mid); + int rightRes = DivideConquer (nums, mid + 1, right); + + if (leftRes == rightRes) { + return leftRes; } - return 0; + int lftCount = GetCount (nums, leftRes, left, right); + int rgtCount = GetCount (nums, rightRes, left, right); + + return lftCount > rgtCount? leftRes : rightRes; } + + public int GetCount (int[] nums, int curNum, int left, int right) { + int count = 0; + for (int i = left; i < right + 1; i++) { + if (curNum == nums[i]) + count++; + } + + return count; + } + } \ No newline at end of file From 901e38002080383e19853c013bff87dcf45030d9 Mon Sep 17 00:00:00 2001 From: Yrp1994 <2927848012@qq.com> Date: Sun, 30 Jun 2019 11:52:20 +0800 Subject: [PATCH 5/7] =?UTF-8?q?[455]=20=E5=88=86=E5=8F=91=E9=A5=BC?= =?UTF-8?q?=E5=B9=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_7/LeetCode_200_455.cs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Week_04/id_7/LeetCode_200_455.cs diff --git a/Week_04/id_7/LeetCode_200_455.cs b/Week_04/id_7/LeetCode_200_455.cs new file mode 100644 index 00000000..a87871a2 --- /dev/null +++ b/Week_04/id_7/LeetCode_200_455.cs @@ -0,0 +1,30 @@ +/* + * @lc app=leetcode.cn id=455 lang=csharp + * + * [455] 分发饼干 + */ +using System; + +public class Solution { + // 解法1:就是先排序,后暴力查找,计数,最后返回计数结果 + public int FindContentChildren (int[] g, int[] s) { + Array.Sort (g); + Array.Sort (s); + int count = 0; + int sIndex = 0; + for (int i = 0; i < g.Length; i++) { + for (int j = sIndex; j < s.Length; j++) { + if (s[j] >= g[i]) { + sIndex = j + 1; + count++; + if (j == s.Length - 1) { + return count; + } + break; + } + } + } + + return count; + } +} \ No newline at end of file From 13591ed0ba1def44719176db1530fe518e2a7c3e Mon Sep 17 00:00:00 2001 From: Yrp1994 <2927848012@qq.com> Date: Sun, 30 Jun 2019 21:04:53 +0800 Subject: [PATCH 6/7] =?UTF-8?q?[784]=20=E5=AD=97=E6=AF=8D=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F=E5=86=99=E5=85=A8=E6=8E=92=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_7/LeetCode_200_169.cs | 2 +- Week_04/id_7/LeetCode_200_455.cs | 2 +- Week_04/id_7/LeetCode_200_720.cs | 2 +- Week_04/id_7/LeetCode_200_784.cs | 71 ++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 Week_04/id_7/LeetCode_200_784.cs diff --git a/Week_04/id_7/LeetCode_200_169.cs b/Week_04/id_7/LeetCode_200_169.cs index a5e89e50..05c41c8b 100644 --- a/Week_04/id_7/LeetCode_200_169.cs +++ b/Week_04/id_7/LeetCode_200_169.cs @@ -6,7 +6,7 @@ using System; using System.Collections.Generic; -public class Solution { +public class Solution2 { // 解法1 // 暴力法,直接双层循环遍历所有数字,统计出现次数,大于nums.Length/2就直接返回 // 优化点:记录之前统计过的数字,再次遇到,则不再统计 diff --git a/Week_04/id_7/LeetCode_200_455.cs b/Week_04/id_7/LeetCode_200_455.cs index a87871a2..7c218b90 100644 --- a/Week_04/id_7/LeetCode_200_455.cs +++ b/Week_04/id_7/LeetCode_200_455.cs @@ -5,7 +5,7 @@ */ using System; -public class Solution { +public class Solution3 { // 解法1:就是先排序,后暴力查找,计数,最后返回计数结果 public int FindContentChildren (int[] g, int[] s) { Array.Sort (g); diff --git a/Week_04/id_7/LeetCode_200_720.cs b/Week_04/id_7/LeetCode_200_720.cs index a427a67d..974afb0a 100644 --- a/Week_04/id_7/LeetCode_200_720.cs +++ b/Week_04/id_7/LeetCode_200_720.cs @@ -6,7 +6,7 @@ using System; using System.Collections.Generic; -public class Solution { +public class Solution1 { // 方法1 // 先排序,后将所有单词放入集合中,遍历每一个单词, // 然后对于每个单词进行每次去除末尾一个字母的循环操作, diff --git a/Week_04/id_7/LeetCode_200_784.cs b/Week_04/id_7/LeetCode_200_784.cs new file mode 100644 index 00000000..9c9315e9 --- /dev/null +++ b/Week_04/id_7/LeetCode_200_784.cs @@ -0,0 +1,71 @@ +/* + * @lc app=leetcode.cn id=784 lang=csharp + * + * [784] 字母大小写全排列 + */ +using System.Collections.Generic; + +public class Solution { + // 方法1:BFS 不是自己想出来的,从国际版上讨论区学习到的 + // public IList LetterCasePermutation (string S) { + // if (S == null) { + // return new List (); + // } + + // Queue que = new Queue (); + // que.Enqueue (S); + + // for (int i = 0; i < S.Length; i++) { + // if (S[i] >= 48 && S[i] <= 57) continue; + // int size = que.Count; + // for (int j = 0; j < size; j++) { + // string cur = que.Dequeue (); + // // 先把自己加进去 + // que.Enqueue (cur); + + // char[] chs = cur.ToCharArray (); + + // if (chs[i] >= 65 && chs[i] <= 90) { + // // 原本大写的话,这里就转小写 + // chs[i] = char.ToLower (chs[i]); + // que.Enqueue (new string (chs)); + // } else { + // // 否则转大写 + // chs[i] = char.ToUpper (chs[i]); + // que.Enqueue (new string (chs)); + // } + // } + // } + + // return new List (que); + // } + + // 方法2:DFS 同样也不是自己想出来的 + public IList LetterCasePermutation (string S) { + if (S == null) { + return new List (); + } + List lstRes = new List (); + dfs (S.ToCharArray (), lstRes, 0); + + return lstRes; + } + + public void dfs (char[] chs, List lst, int index) { + if (index == chs.Length) { + lst.Add (new string (chs)); + return; + } + + if (chs[index] >= '0' && chs[index] <= '9') { + dfs (chs, lst, index + 1); + return; + } + + chs[index] = char.ToLower (chs[index]); + dfs (chs, lst, index + 1); + + chs[index] = char.ToUpper (chs[index]); + dfs (chs, lst, index + 1); + } +} \ No newline at end of file From e599ebf26f95a5fc6b07fd49aae16e67cb8941f5 Mon Sep 17 00:00:00 2001 From: Yrp1994 <2927848012@qq.com> Date: Sun, 30 Jun 2019 21:38:10 +0800 Subject: [PATCH 7/7] =?UTF-8?q?[70]=20=E7=88=AC=E6=A5=BC=E6=A2=AF=20?= =?UTF-8?q?=E7=AC=AC3=E6=AC=A1=E5=81=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Week_04/id_7/LeetCode_200_70.cs | 54 ++++++++++++++++++++++++++++++++ Week_04/id_7/LeetCode_200_784.cs | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 Week_04/id_7/LeetCode_200_70.cs diff --git a/Week_04/id_7/LeetCode_200_70.cs b/Week_04/id_7/LeetCode_200_70.cs new file mode 100644 index 00000000..4f81ee1f --- /dev/null +++ b/Week_04/id_7/LeetCode_200_70.cs @@ -0,0 +1,54 @@ +/* + * @lc app=leetcode.cn id=70 lang=csharp + * + * [70] 爬楼梯 + */ +using System.Collections.Generic; + +public class Solution { + // 方法1: 暴力法+记忆化 + // Dictionary dic = new Dictionary (); + // public int ClimbStairs (int n) { + // if (n == 1) { + // return 1; + // } + + // if (n == 2) { + // return 2; + // } + + // int A; + // if (dic.ContainsKey (n - 1)) { + // A = dic[n - 1]; + // } else { + // A = ClimbStairs (n - 1); + // dic.Add (n - 1, A); + // } + + // int B; + // if (dic.ContainsKey (n - 2)) { + // B = dic[n - 2]; + // } else { + // B = ClimbStairs (n - 2); + // dic.Add (n - 2, B); + // } + + // return A + B; + // } + + // 方法2:动态规划,当然不是我自己想出来的😂 + public int ClimbStairs (int n) { + if (n == 1) { + return 1; + } + + int[] dp = new int[n + 1]; + dp[1] = 1; + dp[2] = 2; + for (int i = 3; i <= n; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + + return dp[n]; + } +} \ No newline at end of file diff --git a/Week_04/id_7/LeetCode_200_784.cs b/Week_04/id_7/LeetCode_200_784.cs index 9c9315e9..2ae47a18 100644 --- a/Week_04/id_7/LeetCode_200_784.cs +++ b/Week_04/id_7/LeetCode_200_784.cs @@ -5,7 +5,7 @@ */ using System.Collections.Generic; -public class Solution { +public class Solution4 { // 方法1:BFS 不是自己想出来的,从国际版上讨论区学习到的 // public IList LetterCasePermutation (string S) { // if (S == null) {