力扣:1206. 设计跳表


import java.util.Arrays;

/**
 * @author xnl
 * @Description:
 * @date: 2022/7/26   21:15
 */
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;
        }
    }

    /**
     * 随机生成层数
     * @return
     */
    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];
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值