一、3407. 子字符串匹配模式
题目链接
字符串匹配问题,把字符串 p 分成两段 、,i 是 ’ * ’ 的下标,判断 s 是否包含这两段,且这两段处于不相交 && 有前后关系
代码如下:
class Solution {
public boolean hasMatch(String s, String p) {
int idx = p.indexOf('*');
int i = s.indexOf(p.substring(0, idx));
return i>=0 && s.substring(i+idx).contains(p.substring(idx+1));
}
}
二、3408. 设计任务管理器
题目链接
使用哈希存当前每个 taskId,对应的 userId 和 priority,再使用堆存储 taskId 和 priority,按照 priority 排序,如果优先级相同,按照 taskId 排序。
- TaskManager(),将数据存入哈希表和堆
- add(),将数据存入哈希表和堆
- edit(),更新哈希表,将更新后的数据存入堆
- rmv(),更新哈希表 execTop(),不断的将数据排出堆,使用懒删除,如果当前数据存储在哈希表中,返回当前的 userId
代码如下:
class TaskManager {
Map<Integer, int[]> map = new HashMap<>();
PriorityQueue<int[]> que = new PriorityQueue<>((x,y)->x[1]==y[1]?y[0]-x[0]:y[1]-x[1]);
public TaskManager(List<List<Integer>> tasks) {
for(List<Integer> x : tasks){
int userId = x.get(0), taskId = x.get(1), priority = x.get(2);
map.put(taskId, new int[]{
userId, priority});
que.offer(new int[]{
taskId, priority});
}
}
public void add(int userId, int taskId, int priority) {
map.put(taskId, new int[]{
userId, priority});
que.offer(new int[]{
taskId, priority});
}
public void edit(int taskId, int newP) {
int[] t = map.get(taskId);
t[1] = newP;
map.put(taskId, t);
que.offer(new int[]{
taskId, t[1]});
}
public void rmv(int taskId) {
map.remove(taskId);
}
public int execTop() {
while(!que.isEmpty()){
int[] t = que.poll();
if(map.containsKey(t[