其他分享
首页 > 其他分享> > LeetCode 1206. 设计跳表

LeetCode 1206. 设计跳表

作者:互联网

在这里插入图片描述

思路:
跳表论文地址:https://www.epaperpress.com/sortsearch/download/skiplist.pdf
题解参考地址:https://leetcode-cn.com/problems/design-skiplist/solution/can-kao-redisshi-xian-by-bakezq/

(用了vector实现,感觉也不能算完全没有使用库函数吧

static const int SKIPLIST_P_VAL = RAND_MAX / 2,MAX_LEVEL = 16;
class Skiplist {
public:
    struct Node {
        int val;
        vector<Node *>level;
        Node(int val,int size = MAX_LEVEL):val(val),level(size){}
    }head;
    int maxlevel = 1;
    Skiplist() :head(INT_MIN,MAX_LEVEL) {}
    
    bool search(int target) {
        Node* prev = _search(target)[0];
        return prev -> level[0] && prev -> level[0] -> val == target;
    }

    vector<Node*> _search(int target) {
        Node* cur = &head;
        vector<Node *>prevs(MAX_LEVEL);

        for(int i = maxlevel - 1;i >= 0;i--) {
            while(cur -> level[i] && cur -> level[i] -> val < target) {
                cur = cur -> level[i];
            }
            prevs[i] = cur;
        }
        return prevs;
    }
    
    void add(int num) {
        auto prevs = _search(num);
        int level = random_level();
        if(level > maxlevel) {
            for(int i = maxlevel;i < level;i++) {
                prevs[i] = &head;
            }
            maxlevel = level;
        }
        Node *cur = new Node(num,level);
        for(int i = level - 1;i >= 0;i--) {
            cur -> level[i] = prevs[i] -> level[i];
            prevs[i] -> level[i] = cur;
        }
    }
    
    bool erase(int num) {
        auto prevs = _search(num);
        if(!prevs[0] -> level[0] || prevs[0] -> level[0] -> val != num) {
            return false;
        }
        Node * del = prevs[0] -> level[0];
        for(int i = 0;i < maxlevel;i++) {
            if(prevs[i] -> level[i] == del) {
                prevs[i] -> level[i] = del -> level[i];
            }
        }
        delete del;
        while(maxlevel > 1 && !head.level[maxlevel - 1]) maxlevel--;
        return true;
    }

    static int random_level() {
        int level = 1;
        while(rand() < SKIPLIST_P_VAL && level < MAX_LEVEL) {
            level++;
        }
        return level;
    }
};

/**
 * Your Skiplist object will be instantiated and called as such:
 * Skiplist* obj = new Skiplist();
 * bool param_1 = obj->search(target);
 * obj->add(num);
 * bool param_3 = obj->erase(num);
 */

标签:1206,num,cur,level,int,prevs,跳表,maxlevel,LeetCode
来源: https://blog.csdn.net/tomjobs/article/details/113786749