modify code

master
algorithmzuo 1 year ago
parent 5cd4837bef
commit 7a4bc68c3f

@ -10,8 +10,11 @@ public class Code01_Trie {
class Trie {
class Node {
// 该节点通过了几次
public int pass;
// 有多少个字符串,以该节点结尾
public int end;
public Node[] nexts;
public Node() {

@ -16,7 +16,10 @@ public class Code03_PrefixAndSuffixSearch {
class WordFilter {
class TrieNode {
// new TrieNode[26]
TrieNode[] nexts;
// pass end 不要!这题不需要!
// 有哪些字符串划过了这个节点,这些字符串的下标,记录!
ArrayList<Integer> 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<Integer> 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<Integer> 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<Integer> small = preList.size() <= sufList.size() ? preList : sufList;
ArrayList<Integer> big = small == preList ? sufList : preList;
for (int i = small.size() - 1; i >= 0; i--) {

@ -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];

@ -17,14 +17,18 @@ public class Code05_MaximumFrequencyStack {
// 出现的最大次数
private int topTimes;
// 每层节点
private HashMap<Integer, ArrayList<Integer>> cntValues = new HashMap<>();
// HashMap -> ArrayList
private HashMap<Integer, ArrayList<Integer>>
cntValues = new HashMap<>();
// 每一个数出现了几次
private HashMap<Integer, Integer> 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<>());

@ -18,8 +18,11 @@ public class Code06_InsertDeleteGetRandom {
public class RandomizedSet {
// v -> i
private HashMap<Integer, Integer> keyIndexMap;
// i -> v
private HashMap<Integer, Integer> 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);

@ -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);
// }
}
Loading…
Cancel
Save