LC 1206. Design Skiplist

link

class Skiplist {
public:
    struct Node{
        int key;
        Node* down;
        Node* next;
        Node(int k, Node* d, Node *n){
            key=k;
            down=d;
            next=n;
        }
    };
    Node* head;
    Skiplist() {
        head=new Node(-1,NULL,NULL);
    }

    bool search(int target) {
        Node* p=head;
        while(p){
            Node* ne=p->next;
            while(ne!=NULL && ne->key<target){
                p=p->next;
                ne=ne->next;
            }
            if(ne!=NULL && target==ne->key) return true;
            p=p->down;
        }
        return false;
    }

    void add(int num) {
        vector<Node*> nodes;
        Node* p=head;
        Node* downNode=NULL;
        while(p){
            Node* ne=p->next;
            while(ne!=NULL && num>ne->key){
                p=p->next;
                ne=ne->next;
            }
            nodes.push_back(p);
            p=p->down;
        }
        for(int i=nodes.size()-1;i>=0;--i){
            auto cur=nodes[i];
            Node* newNode=new Node(num,downNode,NULL);
            newNode->next=cur->next;
            cur->next=newNode;
            downNode=newNode;
            bool up=((rand()&1)==0);
            if(!up) break;
            if(i==0){
                Node* newLayerNode=new Node(num,downNode,NULL);
                head=new Node(-1,head,newLayerNode);
            }
        }
    }

    bool erase(int num) {
        bool flag=false;
        Node* p=head;
        while(p){
            Node* ne=p->next;
            while(ne!=NULL && num>ne->key){
                p=p->next;
                ne=ne->next;
            }
            if(ne!=NULL && num==ne->key){
                p->next=p->next->next;
                flag=true;
            }
            p=p->down;
        }
        return flag;
    }
};

/**
 * 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);
 */

原文地址:https://www.cnblogs.com/FEIIEF/p/12436769.html

时间: 2024-07-31 22:24:06

LC 1206. Design Skiplist的相关文章

[LC] 362. Design Hit Counter

Design a hit counter which counts the number of hits received in the past 5 minutes. Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made to the system in chronological order (ie, the times

[LC] 211. Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

[LC] 170. Two Sum III - Data structure design

Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find - Find if there exists any pair of numbers which sum is equal to the value. Example 1: add(1); add(

Microchip 125 kHz RFID System Design Guide

Passive RFID Basics - AN680 INTRODUCTION Radio Frequency Identification (RFID) systems use radio frequency to identify, locate and track people, assets and animals. Passive RFID systems are composed of three components – a reader (interrogator), pass

HDU 1007 Quoit Design(二分+浮点数精度控制)

Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 47104    Accepted Submission(s): 12318 Problem Description Have you ever played quoit in a playground? Quoit is a game in which fla

如何获取Expression Design 4工具与Expression Blend 4工具

在VS2010+C#+WPF 开发项目过程中涉及到界面的布局与设计,网上有人讲采用Expression Design 4与Expression Blend 4工具相当方便, 于是决定试看看,下面将这个过程与大家分享. 一.安装目的 尽管程序员可以使用VS编写XAML代码的方式来构造用户界面,但是对于有设计爱好的用户来说,使用类似Photoshop一样的Expression套件能将 软件美工最大化.设计过程是先使用了Expression Design来设计图形,然后将其导入到Expression

POJ2100 Graveyard Design(尺取法)

POJ2100 Graveyard Design 题目大意:给定一个数n,求出一段连续的正整数的平方和等于n的方案数,并输出这些方案,注意输出格式: 循环判断条件可以适当剪支,提高效率,(1^2+2^2+..n^2)=n*(n+1)*(2n+1)/6; 尺取时一定要注意循环终止条件的判断. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <

【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

译者地址:[翻]Android Design Support Library 的 代码实验--几行代码,让你的 APP 变得花俏 原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fancy with few lines of code 原文项目 demo: Lab-Android-DesignLibrary 双语对照地址: [翻-双语]Android D

DP什么意思 design pattern 设计模式

DP  design pattern 大话设计模式  中的DP 是设计模式的意思 设计模式的书 ,最经典最原始的就是 GOF 的<设计模式>了. 设计模式的书基本上大多是以这 20 多个模式分开讲.含<大话设计模式> 学了 OOL 写的程序基本上是 OB 的. 只有慢慢掌握了 DP 才能写出真正的 OO 程序. 思想 -> 设计原则 -> DP -> OOD