不使用任何库函数,设计一个 跳表 。
- class Skiplist {
-
- int level=0;
-
- Node head=null;
-
- public Skiplist() {
-
- }
-
- public boolean search(int target) {
- Node cur=head;
- while(cur!=null){
- while(cur.right!=null&&cur.right.val
- cur=cur.right;
- }
- if(cur.right!=null&&cur.right.val==target){
- return true;
- }
- cur=cur.down;
- }
- return false;
- }
-
- public void add(int num) {
- int rLevel=1;
- while(rLevel<=level&&(Math.random()%2)==1){
- rLevel++; // 2
- }
- if(rLevel>level){
- level=rLevel;
- head=new Node(num,null,head);
- }
- Node cur=head,last=null;
- for(int l=level;l>0;l--){
- while(cur.right!=null&&cur.right.val
- cur=cur.right;
- }
- if(l<=rLevel){
- cur.right=new Node(num,cur.right,null);
- if(last!=null){
- last.down=cur.right;
- }
- last=cur.right;
- }
- cur=cur.down;
- }
- }
- // 擦除
- public boolean erase(int num) {
- Node cur=head;
- boolean seen=false;
- for(int l=level;l>0;l--){
- while(cur.right!=null&&cur.right.val
- cur=cur.right;
- }
- if(cur.right!=null&&cur.right.val==num){
- seen=true;
- // Node tmp=cur.right;
- cur.right=cur.right.right;
- }
- cur=cur.down;
- }
- return seen;
- }
- }
-
- /**
- * Your Skiplist object will be instantiated and called as such:
- * Skiplist obj = new Skiplist();
- * boolean param_1 = obj.search(target);
- * obj.add(num);
- * boolean param_3 = obj.erase(num);
- */
-
- public class Node {
- public int val;
- public Node right,down;
-
- Node() {
- }
-
- public Node(int val) {
- this.val = val;
- }
-
- Node(int val, Node right,Node down) {
- this.val = val;
- this.right = right;
- this.down = down;
- }
-
- /*public void add(int value) {
- Node p = this;
- while (p.right != null) {
- p = p.right;
- }
- Node newNode = new Node(value);
- p.right = newNode;
- }
- public void add(Node node) {
- Node p = this;
- while (p.right != null) {
- p = p.right;
- }
- p.right = node;
- }*/
- }
-
相关阅读:
kubeadm v1.20 部署K8S 集群架构
【Pytorch Lighting】第 3 章:使用预训练模型进行迁移学习
MySQL SUBSTRING_INDEX 函数用法
【动手学深度学习】课程笔记 00-03 深度学习介绍及环境配置
-1- threejs 场景常见的方法和属性
DIRECTIVES 配置参数
打开word文档报错,提示HRESULT 0x80004005 位置: 部分: /word/comments.xml,行: 0,列: 0
换根DP总结
SimpleDateFormat严格限制日期转换setLenient(false)
ROS2——分布式通信(十二)
-
原文地址:https://blog.csdn.net/m0_58961367/article/details/133721927