1206

public class Node{
    public Node[]next;//节点在不同层的下一节点,最下面为第一层,包含所有节点值
    public int val;
    public Node(int val,int size){
        this.val=val;
        this.next=new Node[size];
    }
}
class Skiplist {
    private Node head;
    private static final int MAX_LEVEL=32;
    private static final double P=0.25;
    public Skiplist() {
        head=new Node(-1,MAX_LEVEL);
    }
    
    public int randomLevel(){
        int level=1;
        while(Math.random()<P&&level<MAX_LEVEL){
            level++;
        }
        return level;
    }
    
    public boolean search(int target) {
        Node tmp=head;
        for(int i=MAX_LEVEL-1;i>=0;i--){
            while(tmp.next[i]!=null&&tmp.next[i].val<=target){
                tmp=tmp.next[i];
            }
            if(tmp.val==target)return true;
        }
        return false;
    }
    
    public void add(int num) {
        int level=randomLevel();
        Node newNode=new Node(num,level);
        Node tmp=head;
        for(int i=MAX_LEVEL-1;i>=0;i--){
            while(tmp.next[i]!=null&&tmp.next[i].val<num){
                tmp=tmp.next[i];
            }
            if(i>=level)continue;
            newNode.next[i]=tmp.next[i];
            tmp.next[i]=newNode;
        }
    }
    
    public boolean erase(int num) {
        Node tmp=head;
        boolean flag=false;
        for(int i=MAX_LEVEL-1;i>=0;i--){
            while(tmp.next[i]!=null&&tmp.next[i].val<num){
                tmp=tmp.next[i];
            }
            if(tmp.next[i]!=null&&tmp.next[i].val==num){
                Node del=tmp.next[i];
                tmp.next[i]=del.next[i];
                del=null;
                flag=true;
            }
        }
        return flag;
    }
}

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

MAX_LEVEL=32
P=0.25

class Node:
    def __init__(self,val:int,max_level):
        self.val=val
        self.next=[None]*max_level

class Skiplist:
    
    def __init__(self):
        self.head=Node(-1,MAX_LEVEL)
    
    def search(self, target: int) -> bool:
        tmp=self.head
        for i in range(MAX_LEVEL-1,-1,-1):
            while tmp.next[i] and tmp.next[i].val<=target:
                tmp=tmp.next[i]
            if tmp.val==target:
                return True
        return False

    def add(self, num: int) -> None:
        level=self.randomLevel()
        newNode=Node(num,level)
        tmp=self.head
        for i in range(MAX_LEVEL-1,-1,-1):
            while tmp.next[i] and tmp.next[i].val<num:
                tmp=tmp.next[i]
            if i>=level:
                continue
            newNode.next[i]=tmp.next[i]
            tmp.next[i]=newNode

    def erase(self, num: int) -> bool:
        tmp=self.head
        flag=False
        for i in range(MAX_LEVEL-1,-1,-1):
            while tmp.next[i] and tmp.next[i].val<num:
                tmp=tmp.next[i]
            if tmp.next[i] and tmp.next[i].val==num:
                delNode=tmp.next[i]
                tmp.next[i]=delNode.next[i]
                delNode.next[i]=None
                delNode=None
                flag=True
        return flag

    def randomLevel(self) -> int:
        level=1;
        while(random.random()<P and level<MAX_LEVEL):
            level+=1
        return level

# Your Skiplist object will be instantiated and called as such:
# obj = Skiplist()
# param_1 = obj.search(target)
# obj.add(num)
# param_3 = obj.erase(num)

 

posted @ 2022-07-26 13:54  kris-phl  阅读(12)  评论(0)    收藏  举报