码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 1206. 设计跳表


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

    1. class Skiplist {
    2. int level=0;
    3. Node head=null;
    4. public Skiplist() {
    5. }
    6. public boolean search(int target) {
    7. Node cur=head;
    8. while(cur!=null){
    9. while(cur.right!=null&&cur.right.val
    10. cur=cur.right;
    11. }
    12. if(cur.right!=null&&cur.right.val==target){
    13. return true;
    14. }
    15. cur=cur.down;
    16. }
    17. return false;
    18. }
    19. public void add(int num) {
    20. int rLevel=1;
    21. while(rLevel<=level&&(Math.random()%2)==1){
    22. rLevel++; // 2
    23. }
    24. if(rLevel>level){
    25. level=rLevel;
    26. head=new Node(num,null,head);
    27. }
    28. Node cur=head,last=null;
    29. for(int l=level;l>0;l--){
    30. while(cur.right!=null&&cur.right.val
    31. cur=cur.right;
    32. }
    33. if(l<=rLevel){
    34. cur.right=new Node(num,cur.right,null);
    35. if(last!=null){
    36. last.down=cur.right;
    37. }
    38. last=cur.right;
    39. }
    40. cur=cur.down;
    41. }
    42. }
    43. // 擦除
    44. public boolean erase(int num) {
    45. Node cur=head;
    46. boolean seen=false;
    47. for(int l=level;l>0;l--){
    48. while(cur.right!=null&&cur.right.val
    49. cur=cur.right;
    50. }
    51. if(cur.right!=null&&cur.right.val==num){
    52. seen=true;
    53. // Node tmp=cur.right;
    54. cur.right=cur.right.right;
    55. }
    56. cur=cur.down;
    57. }
    58. return seen;
    59. }
    60. }
    61. /**
    62. * Your Skiplist object will be instantiated and called as such:
    63. * Skiplist obj = new Skiplist();
    64. * boolean param_1 = obj.search(target);
    65. * obj.add(num);
    66. * boolean param_3 = obj.erase(num);
    67. */
    68. public class Node {
    69. public int val;
    70. public Node right,down;
    71. Node() {
    72. }
    73. public Node(int val) {
    74. this.val = val;
    75. }
    76. Node(int val, Node right,Node down) {
    77. this.val = val;
    78. this.right = right;
    79. this.down = down;
    80. }
    81. /*public void add(int value) {
    82. Node p = this;
    83. while (p.right != null) {
    84. p = p.right;
    85. }
    86. Node newNode = new Node(value);
    87. p.right = newNode;
    88. }
    89. public void add(Node node) {
    90. Node p = this;
    91. while (p.right != null) {
    92. p = p.right;
    93. }
    94. p.right = node;
    95. }*/
    96. }

  • 相关阅读:
    汽车售后接待vr虚拟仿真实操演练作为岗位培训的重要工具和手段
    深入理解强化学习——马尔可夫决策过程:马尔可夫决策过程和马尔可夫过程/马尔可夫奖励过程的区别
    Python接口自动化测试post请求和get请求,获取请求返回值
    org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
    Setup exvim enviroment
    2023-09-09青少年软件编程(C语言)等级考试试卷(二级)解析
    面试背诵版—操作系统
    如何在 Objective-C 中实现多态性,并且它与其他面向对象编程语言的多态性实现有何差异?
    前端工程化精讲第十三课 缓存优化:那些基于缓存的优化方案
    基于SSM的高校餐厅防疫管理系统
  • 原文地址:https://blog.csdn.net/m0_58961367/article/details/133721927
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号