LeetCode-1206-设计跳表

LeetCode-1206-设计跳表

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

跳表 是在 O(log(n)) 时间内完成增加、删除、搜索操作的数据结构。跳表相比于树堆与红黑树,其功能与性能相当,并且跳表的代码长度相较下更短,其设计思想与链表相似。

例如,一个跳表包含 [30, 40, 50, 60, 70, 90] ,然后增加 8045 到跳表中,以下图的方式操作:

img
Artyom Kalinin [CC BY-SA 3.0], via Wikimedia Commons

跳表中有很多层,每一层是一个短的链表。在第一层的作用下,增加、删除和搜索操作的时间复杂度不超过 O(n)。跳表的每一个操作的平均时间复杂度是 O(log(n)),空间复杂度是 O(n)

了解更多 : https://en.wikipedia.org/wiki/Skip_list

在本题中,你的设计应该要包含这些函数:

  • bool search(int target) : 返回target是否存在于跳表中。
  • void add(int num): 插入一个元素到跳表。
  • bool erase(int num): 在跳表中删除一个值,如果 num 不存在,直接返回false. 如果存在多个 num ,删除其中任意一个即可。

注意,跳表中可能存在多个相同的值,你的代码需要处理这种情况。

示例 1:

输入
["Skiplist", "add", "add", "add", "search", "add", "search", "erase", "erase", "search"]
[[], [1], [2], [3], [0], [4], [1], [0], [1], [1]]
输出
[null, null, null, null, false, null, true, false, true, false]

解释
Skiplist skiplist = new Skiplist();
skiplist.add(1);
skiplist.add(2);
skiplist.add(3);
skiplist.search(0);   // 返回 false
skiplist.add(4);
skiplist.search(1);   // 返回 true
skiplist.erase(0);    // 返回 false,0 不在跳表中
skiplist.erase(1);    // 返回 true
skiplist.search(1);   // 返回 false,1 已被擦除

提示:

  • 0 <= num, target <= 2 * 104
  • 调用search, add, erase操作次数不大于 5 * 104
class Skiplist {
    class SkipNode {
        int val;
        /**
         * 每一个层级的指针
         */
        SkipNode[] next;

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

    /**
     * 建立的调表所能达到的最高层级
     */
    private final int MAX_LEVEL = 32;
    /**
     * 概率因子
     */
    private final double SKIPLIST_P = 0.25;
    private SkipNode head;
    /**
     * 当前建立的最高层级
     */
    private int curLevel;

    public Skiplist() {
        head = new SkipNode(Integer.MIN_VALUE, MAX_LEVEL);
        curLevel = 0;
    }

    /**
     * 添加节点时, 该节点能达到的层数
     */
    private int getRandomLevel() {
        int level = 0;
        while (Math.random() < SKIPLIST_P && level < MAX_LEVEL) {
            level++;
        }
        return level;
    }

    /**
     * 从最高层级开始查找, 如果当前节点小于查找节点, 往右走, 否则往下走
     */
    public boolean search(int target) {
        SkipNode cur = head;
        for (int level = curLevel; level >= 0; level--) {
            // 找到第0层小于target的最大值
            while (cur.next[level] != null && cur.next[level].val < target) {
                cur = cur.next[level];
            }
        }
        // 指向下一个位置
        cur = cur.next[0];
        return cur != null && cur.val == target;
    }

    /**
     * 先找到每一个层级的小于 num 的最大节点, 即前继节点
     * 然后获取插入节点能到达的最高层级
     * 最后将 num 节点插入即可
     */
    public void add(int num) {
        SkipNode[] temp = new SkipNode[MAX_LEVEL];
        Arrays.fill(temp, head);
        SkipNode cur = head;
        // 找到所有层级的小于插入节点的最大节点
        for (int level = curLevel; level >= 0; level--) {
            // 找到第0层小于target的最大值
            while (cur.next[level] != null && cur.next[level].val < num) {
                cur = cur.next[level];
            }
            temp[level] = cur;
        }

        // 获取新插入节点所能达到的最高层级
        int randomLevel = getRandomLevel();
        SkipNode newNode = new SkipNode(num, randomLevel + 1);
        // 插入节点
        for (int i = 0; i <= randomLevel; i++) {
            newNode.next[i] = temp[i].next[i];
            temp[i].next[i] = newNode;
        }
    }

    /**
     * 先找到每一个层级的小于 num 的最大节点, 即前继节点
     * 然后判断是否存在该 num 节点, 不存在返回 false 即可
     * 存在则从第 0 层开始删除, 当某个层级找不到 num 节点时, 不需要继续往上层找
     * 判断删除 num 节点后, 最高层是否只剩下 head 节点, 如果是, 层级减一
     */
    public boolean erase(int num) {
        SkipNode[] temp = new SkipNode[MAX_LEVEL];
        Arrays.fill(temp, head);
        SkipNode cur = head;
        // 找到所有层级的小于插入节点的最大节点
        for (int level = curLevel; level >= 0; level--) {
            // 找到第0层小于target的最大值
            while (cur.next[level] != null && cur.next[level].val < num) {
                cur = cur.next[level];
            }
            temp[level] = cur;
        }

        cur = cur.next[0];
        if (cur == null || cur.val != num) {
            return false;
        }

        for (int i = 0; i <= curLevel; i++) {
            // 如果低层没有目标节点, 高层也不会有, 结束即可
            if (temp[i].next[i] != cur) {
                break;
            }
            temp[i].next[i] = cur.next[i];
        }

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

指针版 有问题 日后修复

public class SkipNode<T> {
    int key;
    T value;
    SkipNode<T> right, down;
    public SkipNode(int key, T value) {
        this.key = key;
        this.value = value;
    }
}
public class SkipList<T> {
    /**
     * 头节点, 入口
     */
    private SkipNode<T> head;
    /**
     * 当前跳表索引层数
     */
    private int highLevel;
    /**
     * 用于投掷硬币
     */
    private Random random;
    /**
     * 最大层级
     */
    private final int MAX_LEVEL = 32;

    public SkipList() {
        random = new Random();
        head = new SkipNode<>(Integer.MIN_VALUE, null);
        highLevel = 0;
    }

    public SkipNode<T> search(int key) {
        SkipNode<T> cur = head;
        while (cur != null) {
            if (cur.key == key) {
                // 找到相应key
                return cur;
            } else if (cur.right == null || cur.right.key > key) {
                // 当前层次已经走到最后, 或者当前节点大于key, 往下层查找
                cur = cur.down;
            } else {
                // 右侧小于key
                cur = cur.right;
            }
        }
        return null;
    }

    public boolean erase(int key) {
        boolean res = false;
        SkipNode<T> cur = head;
        while (cur != null) {
            if (cur.right == null) {
                // 右边为空, 往下找
                cur = cur.down;
            } else if (cur.right.key == key) {
                res = true;
                // 删除当前层次的key节点, 继续往下一层找
                SkipNode<T> right = cur.right;
                cur.right = right.right;
                right.right = right.down = null;
            } else if (cur.right.key > key) {
                // 当前层级右侧不可能存在key节点
                cur = cur.down;
            } else {
                // 当前层级右侧节点小于key, 往右边找
                cur = cur.right;
            }
        }
        return res;
    }

    public void add(SkipNode<T> node) {
        int key = node.key;
        SkipNode<T> findNode = search(key);
        if(findNode != null) {
            //如果存在这个key的节点
            findNode.value=node.value;
            return;
        }
        SkipNode<T> cur = head;
        Stack<SkipNode<T>> stack = new Stack<>();
        // 找到每一个层级的待插入节点的左节点
        while (cur != null) {
            if (cur.right == null || cur.right.key > key) {
                // 如果右侧节点为空或者右侧节点的大于key节点, 往左走
                stack.add(cur);
                cur = cur.down;
            } else {
                // 右侧节点小于左侧节点, 往右走
                cur = cur.right;
            }
        }
        // 从第一层开始插入
        int level = 1;
        // 记录插入当前层级的插入节点, 以便在上一层插入时进行连接
        SkipNode<T> downNode = null;
        while (!stack.isEmpty()) {
            SkipNode<T> temp = stack.pop();

            // 创建新节点, 并连接新节点
            SkipNode<T> newNode = new SkipNode<>(node.key, node.value);
            newNode.down = downNode;
            // downNode记录当前节点
            downNode = newNode;
            if (temp.right == null) {
                temp.right = newNode;
            } else {
                newNode.right = temp.right;
                temp.right = newNode;
            }

            // 判断是否还需要向上插入
            if (level > MAX_LEVEL) {
                break;
            }
            // 概率插入
            if (random.nextDouble() > 0.5) {
                break;
            }
            level++;
            // 如果当前最大高度还要大(即新增了一层), 需要改变head节点
            if (level > highLevel) {
                highLevel = level;
                // 需要创建一个新的节点
                SkipNode<T> newHead = new SkipNode<>(Integer.MIN_VALUE, null);
                newHead.down = head;
                // 改变head
                head = newHead;
                stack.add(head);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LeetCode-Editor是一种在线编码工具,它提供了一个用户友好的界面编写和运行代码。在使用LeetCode-Editor时,有时候会出现乱码的问题。 乱码的原因可能是由于编码格式不兼容或者编码错误导致的。在这种情况下,我们可以尝试以下几种解决方法: 1. 检查文件编码格式:首先,我们可以检查所编辑的文件的编码格式。通常来说,常用的编码格式有UTF-8和ASCII等。我们可以将编码格式更改为正确的格式。在LeetCode-Editor中,可以通过界面设置或编辑器设置来更改编码格式。 2. 使用正确的字符集:如果乱码是由于使用了不同的字符集导致的,我们可以尝试更改使用正确的字符集。常见的字符集如Unicode或者UTF-8等。在LeetCode-Editor中,可以在编辑器中选择正确的字符集。 3. 使用合适的编辑器:有时候,乱码问题可能与LeetCode-Editor自身相关。我们可以尝试使用其他编码工具,如Text Editor、Sublime Text或者IDE,看是否能够解决乱码问题。 4. 查找特殊字符:如果乱码问题只出现在某些特殊字符上,我们可以尝试找到并替换这些字符。通过仔细检查代码,我们可以找到导致乱码的特定字符,并进行修正或替换。 总之,解决LeetCode-Editor乱码问题的方法有很多。根据具体情况,我们可以尝试更改文件编码格式、使用正确的字符集、更换编辑器或者查找并替换特殊字符等方法来解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值