栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

力扣:1206. 设计跳表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

力扣:1206. 设计跳表

import java.util.Arrays;


public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        Skiplist skiplist = new Skiplist();
        skiplist.add(1);
        skiplist.add(2);
        skiplist.add(3);
        System.out.println(skiplist.search(0));
        skiplist.add(4);
        System.out.println(skiplist.search(1));
        System.out.println(skiplist.erase(0));
        System.out.println(skiplist.erase(1));
        System.out.println(skiplist.search(1));
    }


}

class Skiplist {

    private static final int MAX_LEVEL = 32;
    private static final double SKIPLIST_P = 0.25;
    private Node head;
    private int curLevel;

    // 初始化
    public Skiplist() {
        this.head = new Node(-1, MAX_LEVEL);
        this.curLevel = 0;
    }

    public boolean search(int target) {
        Node cur = this.head;
        for (int i = curLevel - 1; i >= 0; i--){
            // 找到第 i 最大的小于target的元素
            while (cur.next[i] != null && cur.next[i].val < target){
                cur = cur.next[i];
            }
        }

        // 已经在第一层
        cur = cur.next[0];
        return cur != null && cur.val == target;
    }

    public void add(int num) {
        // 存放更新的位置
        Node[] update = new Node[MAX_LEVEL];
        Arrays.fill(update, head);
        Node cur = this.head;
        for (int i = curLevel - 1; i >= 0; i--){
            while (cur.next[i] != null && cur.next[i].val < num){
                cur = cur.next[i];
            }
            update[i] = cur;
        }
        int randomLevel = randomLevel();
        this.curLevel = Math.max(this.curLevel, randomLevel);
        Node newNode = new Node(num, randomLevel);
        // 插入随机出来的level
        for (int i = 0; i < randomLevel; i++){
            newNode.next[i] = update[i].next[i];
            update[i].next[i] = newNode;
        }
    }

    
    private int randomLevel(){
        int level = 1;
        while (Math.random() < SKIPLIST_P && level < MAX_LEVEL){
            level++;
        }
        return level;
    }

    public boolean erase(int num) {
        Node[] update = new Node[MAX_LEVEL];
        Node cur = this.head;
        for (int i = curLevel - 1; i >= 0; i--){
            while (cur.next[i] != null && cur.next[i].val < num){
                cur = cur.next[i];
            }
            update[i] = cur;
        }
        cur = cur.next[0];
        if (cur == null || cur.val != num){
            return false;
        }

        for (int i = 0; i < curLevel; i++){
            if (update[i].next[i] != cur){
                break;
            }
            update[i].next[i] = cur.next[i];
        }

        while (curLevel > 1 && head.next[curLevel - 1] == null){
            curLevel--;
        }
        return true;
    }
}

class Node{
    int val;
    Node[] next;

    public Node(int val, int maxLevel){
        this.val = val;
        this.next = new Node[maxLevel];
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1026041.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号