力扣: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];
    }
}

你可能感兴趣的:(菜鸟刷题记录,算法,算法,java,c++)