当前位置:网站首页>【Leetcode】1206. Design Skiplist
【Leetcode】1206. Design Skiplist
2022-08-01 23:47:00 【记录算法题解】
题目地址:
https://leetcode.com/problems/design-skiplist/
实现一个跳表。
跳表如下图所示:
其是一个分层有序链表结构(有序指的是按元素大小排序,类似于平衡树),设最下层为第 0 0 0层,向上的层数以此类推。每一层的节点数大致是其上面一层的两倍,而查找元素的时候可以通过高层的快速跳跃实现优化。有个虚拟头结点为搜索的出发节点。核心操作为“前驱查找”,这个操作会找到每一层的小于给定元素 x x x的最后一个节点(如果不存在,则视为找到了头结点)。
每个操作具体如下:
1、查找 x x x是否存在。先对 x x x进行前驱查找,然后看第 0 0 0层找到的那个节点的下一个节点,只需要看其是否值为 x x x即可;
2、添加 x x x。先对 x x x进行前驱查找,然后看第 0 0 0层找到的那个节点,如果那个节点存在,则其为小于 x x x的最后一个节点;如果不存在,那个节点即为头结点。无论怎样,都可以在那个节点之后添加新的值为 x x x的节点。此时,以 50 % 50\% 50%的概率将这个节点插到上一层,插完之后再以 50 % 50\% 50%的概率将这个节点插到更上一层,一直插到最上层或中间停止了为止。
3、删除 x x x。先对 x x x进行前驱查找,然后看第 0 0 0层找到的那个节点,看其下一个节点,如果不存在,或者值不为 x x x,则说明 x x x不存在,直接返回。否则说明找到了,删除之,并向上爬,删除上面层中的 x x x。
代码如下:
class Skiplist {
public:
#define level 8
struct Node {
int val;
vector<Node*> next;
Node(int val) : val(val) {
next.resize(level, nullptr);
}
} *head;
Skiplist() {
head = new Node(-1);
}
~Skiplist() {
delete head;
}
void find(int x, vector<Node*> &pre) {
// 从头结点出发,找到每一层小于x的最后一个节点,如果没找到则赋为头结点
auto p = head;
for (int i = level - 1; i >= 0; i--) {
while (p->next[i] && p->next[i]->val < x) p = p->next[i];
pre[i] = p;
}
}
bool search(int x) {
vector<Node*> pre(level);
find(x, pre);
auto p = pre[0]->next[0];
return p && p->val == x;
}
void add(int x) {
vector<Node*> pre(level);
find(x, pre);
auto p = new Node(x);
// 从下向上每一层添加节点x
for (int i = 0; i < level; i++) {
p->next[i] = pre[i]->next[i];
pre[i]->next[i] = p;
// 以50%的概率退出
if (rand() & 1) break;
}
}
bool erase(int x) {
vector<Node*> pre(level);
find(x, pre);
auto p = pre[0]->next[0];
if (!p || p->val != x) return false;
for (int i = 0; i < level && pre[i]->next[i] == p; i++)
pre[i]->next[i] = p->next[i];
delete p;
return true;
}
};
每个操作平均时间复杂度 O ( log n ) O(\log n) O(logn), n n n为表里已有的元素个数(即最下面一层的节点个数),空间平均 O ( n ) O(n) O(n)。
边栏推荐
- JAX-based activation function, softmax function and cross entropy function
- FAST-LIO2代码解析(二)
- 技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
- Sql之各种Join
- LocalDateTime转为Date类型
- PostgreSQL Basics--Common Commands
- yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
- @Resource和@Autowired的区别
- How do programmers solve online problems gracefully?
- YOLO等目标检测模型的非极大值抑制NMS和评价指标(Acc, Precision, Recall, AP, mAP, RoI)、YOLOv5中[email protected]与
猜你喜欢
机器学习文本分类
在MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password YES) 拒绝访问问题解决
数据机构---第五章树与二叉树---二叉树的概念---应用题
cmd command
架构基本概念和架构本质
仿牛客网项目第三章:开发社区核心功能(详细步骤和思路)
A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
软件测试之移动APP安全测试简析,北京第三方软件检测机构分享
在MySQL中使用MD5加密【入门体验】
Architecture basic concept and nature of architecture
随机推荐
Making a Simple 3D Renderer
DRF generating serialization class code
如何进行数据库备份
问题解决方式了
Work for 5 years, test case design is bad?To look at the big case design summary
[Camp Experience Post] 2022 Cybersecurity Summer Camp
斜堆、、、
Special characters & escapes in bat
oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed
Programmer is still short of objects? A new one is enough
color transparency parameter
Classical Literature Reading--DLO
12306抢票,极限并发带来的思考?
Several interview questions about golang concurrency
WEB安全基础 - - - XRAY使用
中职网络安全竞赛B7比赛部署流程
Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights
一款简洁的文件传输工具
[LeetCode304 Weekly Competition] Two questions about the base ring tree 6134. Find the closest node to the given two nodes, 6135. The longest cycle in the graph
程序员还差对象?new一个就行了