当前位置: 首页 > news >正文

1206. 设计跳表

不使用任何库函数,设计一个 跳表 。

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<target){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<num){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<num){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;}*/
}

http://www.lryc.cn/news/186963.html

相关文章:

  • 【API要返回一棵树的结构】数据库表结构是平铺的数据,但是api要实现树状结构展示。api实现一棵树的结构,如何实现呢,递归?如何递归呢
  • 视频批量剪辑工具,自定义视频速率,批量剪辑工具助力创意无限”
  • starrocks启动和停止和重启脚本
  • 升级Xcode 15后,出现大量Duplicate symbols问题
  • Godot2D角色导航教程(角色随鼠标移动)
  • 论文阅读--Cell-free massive MIMO versus small cells
  • 【深度学习】UniControl 一个统一的扩散模型用于可控的野外视觉生成
  • 使用ChatGPT和MindShow一分钟生成PPT模板
  • C#对字典容器Dictionary<TKey, TValue>内容进行XML序列化或反序列化报错解决方法
  • 【Linux】Linux 之用户管理
  • NLP:Attention和self-attention的区别
  • Gap Year Plan
  • 厌烦了iPhone默认的热点名称?如何更改iPhone上的热点名称
  • 【数据库审计】2023年数据库审计厂家汇总
  • C#WPF StackPanel布局及Border边框应用实例
  • RabbitMQ-第四种交换机类型
  • Redis AOF重写原原理
  • es6.x和es7.x如何创建索引?
  • 《DevOps 精要:业务视角》- 读书笔记(三)
  • C语言——文件操作_学习笔记
  • cap分布式理论
  • asp.net core 如何统一json序列化格式
  • DALL·E 3 ChatGPT-4的梦幻联动
  • linux,write:xxx has messages disabled 与 Ubuntu多用户同时登录的问题 ubuntu 20.04
  • ffmpeg批量转换ape/wav为mp3 (linux, mac适用)
  • 自动生成JPA bean及repository生成简陋工具
  • vue3+vite+uniapp 封装一个省市区组件
  • OpenCV报错:AttributeError: module ‘cv2.cv2‘ has no attribute ‘SIFT_create‘
  • 通用监控视频web播放方案
  • C++基础知识3