Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions Week_04/id_7/LeetCode_200_169.cs
Original file line number Diff line number Diff line change
@@ -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<int> hs = new HashSet<int> ();
// 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<int, int> dicCount = new Dictionary<int, int> ();
// 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;
}

}
30 changes: 30 additions & 0 deletions Week_04/id_7/LeetCode_200_455.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
54 changes: 54 additions & 0 deletions Week_04/id_7/LeetCode_200_70.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* @lc app=leetcode.cn id=70 lang=csharp
*
* [70] 爬楼梯
*/
using System.Collections.Generic;

public class Solution {
// 方法1: 暴力法+记忆化
// Dictionary<int, int> dic = new Dictionary<int, int> ();
// 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];
}
}
120 changes: 120 additions & 0 deletions Week_04/id_7/LeetCode_200_720.cs
Original file line number Diff line number Diff line change
@@ -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<string> hsWord = new HashSet<string> ();
// 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<char, Node> children = new Dictionary<char, Node> ();
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<Node> sk = new Stack<Node> ();
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 ();
}
}
Loading