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..05c41c8b --- /dev/null +++ b/Week_04/id_7/LeetCode_200_169.cs @@ -0,0 +1,119 @@ +/* + * @lc app=leetcode.cn id=169 lang=csharp + * + * [169] 求众数 + */ +using System; +using System.Collections.Generic; + +public class Solution2 { + // 解法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) { + 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; + } + + 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 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..7c218b90 --- /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 Solution3 { + // 解法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 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_720.cs b/Week_04/id_7/LeetCode_200_720.cs new file mode 100644 index 00000000..974afb0a --- /dev/null +++ b/Week_04/id_7/LeetCode_200_720.cs @@ -0,0 +1,120 @@ +/* + * @lc app=leetcode.cn id=720 lang=csharp + * + * [720] 词典中最长的单词 + */ +using System; +using System.Collections.Generic; + +public class Solution1 { + // 方法1 + // 先排序,后将所有单词放入集合中,遍历每一个单词, + // 然后对于每个单词进行每次去除末尾一个字母的循环操作, + // 如果可以执行到单词长度为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; + // } + + // 方法2 + // 构建字典树(Trie),再使用深度优先遍历(dfs)遍历所有节点,找到最长单词 + // 注意点:需要存储字典树中每个节点对应的单词的位置,即代码中end,这样在深度优先比例时,可以直接从数组中取得对应单词 + // dfs,当前代码使用的是非递归,也可以使用递归 + // 此代码从国际版的官方解法中获得 + class Node { + char c; + public Dictionary children = new Dictionary (); + public int end; + public Node (char c) { + this.c = c; + } + } + + class Trie { + Node root; + public string[] words; + + public Trie () { + root = new Node ('0'); + } + + 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]]; + } + + 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; + } + 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 trie.dfs (); + } +} \ 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 new file mode 100644 index 00000000..2ae47a18 --- /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 Solution4 { + // 方法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