diff --git a/MCA算法突击课/第03期/mca_08/Code01_Trie.java b/MCA算法突击课/第03期/mca_08/Code01_Trie.java index 70bb809..934de06 100644 --- a/MCA算法突击课/第03期/mca_08/Code01_Trie.java +++ b/MCA算法突击课/第03期/mca_08/Code01_Trie.java @@ -10,8 +10,11 @@ public class Code01_Trie { class Trie { class Node { + // 该节点通过了几次 public int pass; + // 有多少个字符串,以该节点结尾 public int end; + public Node[] nexts; public Node() { diff --git a/MCA算法突击课/第03期/mca_08/Code03_PrefixAndSuffixSearch.java b/MCA算法突击课/第03期/mca_08/Code03_PrefixAndSuffixSearch.java index 485f352..b20b987 100644 --- a/MCA算法突击课/第03期/mca_08/Code03_PrefixAndSuffixSearch.java +++ b/MCA算法突击课/第03期/mca_08/Code03_PrefixAndSuffixSearch.java @@ -16,7 +16,10 @@ public class Code03_PrefixAndSuffixSearch { class WordFilter { class TrieNode { + // new TrieNode[26] TrieNode[] nexts; + // pass end 不要!这题不需要! + // 有哪些字符串划过了这个节点,这些字符串的下标,记录! ArrayList indies; public TrieNode() { @@ -29,10 +32,12 @@ public class Code03_PrefixAndSuffixSearch { TrieNode sufHead; + // 初始化,有所有的字符串! public WordFilter(String[] words) { preHead = new TrieNode(); sufHead = new TrieNode(); for (int i = 0; i < words.length; i++) { + // 所有字符串,i String word = words[i]; TrieNode cur = preHead; for (int j = 0; j < word.length(); j++) { @@ -55,29 +60,31 @@ public class Code03_PrefixAndSuffixSearch { } } + // 给前缀 pref + // 给后缀 suff public int f(String pref, String suff) { ArrayList preList = null; TrieNode cur = preHead; for (int i = 0; i < pref.length() && cur != null; i++) { cur = cur.nexts[pref.charAt(i) - 'a']; } + if(cur == null) { + return -1; + } if (cur != null) { preList = cur.indies; } - if (preList == null) { - return -1; - } ArrayList sufList = null; cur = sufHead; for (int i = suff.length() - 1; i >= 0 && cur != null; i--) { cur = cur.nexts[suff.charAt(i) - 'a']; } + if(cur == null) { + return -1; + } if (cur != null) { sufList = cur.indies; } - if (sufList == null) { - return -1; - } ArrayList small = preList.size() <= sufList.size() ? preList : sufList; ArrayList big = small == preList ? sufList : preList; for (int i = small.size() - 1; i >= 0; i--) { diff --git a/MCA算法突击课/第03期/mca_08/Code04_SameTeams.java b/MCA算法突击课/第03期/mca_08/Code04_SameTeams.java index 5126ab5..156a391 100644 --- a/MCA算法突击课/第03期/mca_08/Code04_SameTeams.java +++ b/MCA算法突击课/第03期/mca_08/Code04_SameTeams.java @@ -29,6 +29,7 @@ public class Code04_SameTeams { TrieNode root = new TrieNode(); TrieNode cur = null; for (int i = 0; i < as.length; i++) { + // as[i] -> {4, 7, 3, 9} cur = root; for (int j = 1; j < as[i].length; j++) { int diff = as[i][j] - as[i][j - 1]; @@ -41,6 +42,8 @@ public class Code04_SameTeams { } int[] ans = new int[bs.length]; for (int i = 0; i < bs.length; i++) { + // bs[i] = {4 5 9 3 2 ..} + // ans[i] = 0个 cur = root; for (int j = 1; j < bs[i].length; j++) { int diff = bs[i][j] - bs[i][j - 1]; diff --git a/MCA算法突击课/第03期/mca_08/Code05_MaximumFrequencyStack.java b/MCA算法突击课/第03期/mca_08/Code05_MaximumFrequencyStack.java index 5cd1303..c2f84c9 100644 --- a/MCA算法突击课/第03期/mca_08/Code05_MaximumFrequencyStack.java +++ b/MCA算法突击课/第03期/mca_08/Code05_MaximumFrequencyStack.java @@ -17,14 +17,18 @@ public class Code05_MaximumFrequencyStack { // 出现的最大次数 private int topTimes; // 每层节点 - private HashMap> cntValues = new HashMap<>(); + // HashMap -> ArrayList + private HashMap> + cntValues = new HashMap<>(); + + // 每一个数出现了几次 private HashMap valueTopTime = new HashMap<>(); public void push(int val) { // 当前数词频+1 valueTopTime.put(val, valueTopTime.getOrDefault(val, 0) + 1); - // 当前数是什么词频 5 7次 + // 当前数是什么词频 int curTopTimes = valueTopTime.get(val); if (!cntValues.containsKey(curTopTimes)) { cntValues.put(curTopTimes, new ArrayList<>()); diff --git a/MCA算法突击课/第03期/mca_08/Code06_InsertDeleteGetRandom.java b/MCA算法突击课/第03期/mca_08/Code06_InsertDeleteGetRandom.java index fe3e4c0..4def3be 100644 --- a/MCA算法突击课/第03期/mca_08/Code06_InsertDeleteGetRandom.java +++ b/MCA算法突击课/第03期/mca_08/Code06_InsertDeleteGetRandom.java @@ -18,8 +18,11 @@ public class Code06_InsertDeleteGetRandom { public class RandomizedSet { + // v -> i private HashMap keyIndexMap; + // i -> v private HashMap indexKeyMap; + // 几个数进来了 private int size; public RandomizedSet() { @@ -38,9 +41,15 @@ public class Code06_InsertDeleteGetRandom { } public boolean remove(int val) { + // 拿最后的位置,填上当前要删掉数字的,洞! if (keyIndexMap.containsKey(val)) { + // 删掉 30 7 + // size-1位置的数字,填洞 + // 查出30在7位置 int deleteIndex = keyIndexMap.get(val); + // 最后位置是啥?size - 1 int lastIndex = --size; + // x lastKey int lastKey = indexKeyMap.get(lastIndex); keyIndexMap.put(lastKey, deleteIndex); indexKeyMap.put(deleteIndex, lastKey); diff --git a/MCA算法突击课/第03期/mca_08/Test.java b/MCA算法突击课/第03期/mca_08/Test.java new file mode 100644 index 0000000..1027ba2 --- /dev/null +++ b/MCA算法突击课/第03期/mca_08/Test.java @@ -0,0 +1,96 @@ +package 第03期.mca_08; + +public class Test { + + class Trie { + + class Node { + public int pass; + public int end; + public Node[] nexts; + + public Node() { + pass = 0; + end = 0; + nexts = new Node[26]; + } + } + + private Node root; + + public Trie() { + root = new Node(); + } + + // 在该结构里,加入str,一次 + public void insert(String str) { + Node cur = root; + cur.pass++; + for (int i = 0; i < str.length(); i++) { + int path = str.charAt(i) - 'a'; + if (cur.nexts[path] == null) { + cur.nexts[path] = new Node(); + } + // 往下跳! + cur = cur.nexts[path]; + cur.pass++; + } + cur.end++; + } + + // 在该结构里,删掉str,一次 + public void erase(String str) { + if (countWordsEqualTo(str) != 0) { + Node cur = root; + cur.pass--; + for (int i = 0; i < str.length(); i++) { + int path = str.charAt(i) - 'a'; + Node next = cur.nexts[path]; + if (next.pass == 1) { + cur.nexts[path] = null; + return; + } else { + cur = cur.nexts[path]; + } + cur.pass--; + } + cur.end--; + } + } + + // 给你一个字符串,str,查一下str出现了几次 + public int countWordsEqualTo(String str) { + Node cur = root; + for (int i = 0; i < str.length(); i++) { + int path = str.charAt(i) - 'a'; + if (cur.nexts[path] == null) { + return 0; + } + // 往下跳! + cur = cur.nexts[path]; + } + return cur.end; + } + + // 给你一个字符串,str,查一下有多少个字符串以str开头 + public int countWordsStartingWith(String str) { + Node cur = root; + for (int i = 0; i < str.length(); i++) { + int path = str.charAt(i) - 'a'; + if (cur.nexts[path] == null) { + return 0; + } + // 往下跳! + cur = cur.nexts[path]; + } + return cur.pass; + } + } + +// public static void main(String[] args) { +// char cur = 'z'; +// int path = cur - 'a'; +// System.out.println(path); +// } + +} diff --git a/MCA算法突击课/第03期/ppt/key/第08节.key b/MCA算法突击课/第03期/ppt/key/第08节.key new file mode 100644 index 0000000..6011cde Binary files /dev/null and b/MCA算法突击课/第03期/ppt/key/第08节.key differ diff --git a/MCA算法突击课/第03期/ppt/powerpoint/第08节.pptx b/MCA算法突击课/第03期/ppt/powerpoint/第08节.pptx new file mode 100644 index 0000000..756cc7b Binary files /dev/null and b/MCA算法突击课/第03期/ppt/powerpoint/第08节.pptx differ