parent
2d9b349407
commit
782e9d8440
@ -0,0 +1,54 @@
|
||||
package 第03期.mca_07;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
// 设计一个类似堆栈的数据结构,将元素推入堆栈,并从堆栈中弹出出现频率最高的元素。
|
||||
// 实现 FreqStack 类:
|
||||
// FreqStack() 构造一个空的堆栈。
|
||||
// void push(int val) 将一个整数 val 压入栈顶。
|
||||
// int pop() 删除并返回堆栈中出现频率最高的元素。
|
||||
// 如果出现频率最高的元素不只一个,则移除并返回最接近栈顶的元素。
|
||||
// 测试链接 : https://leetcode.cn/problems/maximum-frequency-stack/
|
||||
public class Code05_MaximumFrequencyStack {
|
||||
|
||||
class FreqStack {
|
||||
|
||||
// 出现的最大次数
|
||||
private int topTimes;
|
||||
// 每层节点
|
||||
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<>());
|
||||
}
|
||||
ArrayList<Integer> curTimeValues = cntValues.get(curTopTimes);
|
||||
curTimeValues.add(val);
|
||||
topTimes = Math.max(topTimes, curTopTimes);
|
||||
}
|
||||
|
||||
public int pop() {
|
||||
// 最大词频的那一层的链表(动态数组)
|
||||
ArrayList<Integer> topTimeValues = cntValues.get(topTimes);
|
||||
int ans = topTimeValues.remove(topTimeValues.size() - 1);
|
||||
if (topTimeValues.size() == 0) {
|
||||
cntValues.remove(topTimes--);
|
||||
}
|
||||
int times = valueTopTime.get(ans);
|
||||
if (times == 1) {
|
||||
valueTopTime.remove(ans);
|
||||
} else {
|
||||
valueTopTime.put(ans, times - 1);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package 第03期.mca_07;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
// 测试链接 : https://leetcode.com/problems/insert-delete-getrandom-o1/
|
||||
public class Code06_InsertDeleteGetRandom {
|
||||
|
||||
public class RandomizedSet {
|
||||
|
||||
private HashMap<Integer, Integer> keyIndexMap;
|
||||
private HashMap<Integer, Integer> indexKeyMap;
|
||||
private int size;
|
||||
|
||||
public RandomizedSet() {
|
||||
keyIndexMap = new HashMap<Integer, Integer>();
|
||||
indexKeyMap = new HashMap<Integer, Integer>();
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public boolean insert(int val) {
|
||||
if (!keyIndexMap.containsKey(val)) {
|
||||
keyIndexMap.put(val, size);
|
||||
indexKeyMap.put(size++, val);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean remove(int val) {
|
||||
if (keyIndexMap.containsKey(val)) {
|
||||
int deleteIndex = keyIndexMap.get(val);
|
||||
int lastIndex = --size;
|
||||
int lastKey = indexKeyMap.get(lastIndex);
|
||||
keyIndexMap.put(lastKey, deleteIndex);
|
||||
indexKeyMap.put(deleteIndex, lastKey);
|
||||
keyIndexMap.remove(val);
|
||||
indexKeyMap.remove(lastIndex);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getRandom() {
|
||||
if (size == 0) {
|
||||
return -1;
|
||||
}
|
||||
int randomIndex = (int) (Math.random() * size);
|
||||
return indexKeyMap.get(randomIndex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package 第03期.mca_07;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class ReadLine {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
System.out.println("请输入文本");
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
|
||||
String line = null;
|
||||
while ((line = in.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue