国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

380. Insert Delete GetRandom O(1)

2019-11-08 20:07:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Design a data structure that supports all following Operations in average O(1) time.

insert(val): Inserts an item val to the set if not already PResent.remove(val): Removes an item val from the set if present.getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.

Example:

// Init an empty set.RandomizedSet randomSet = new RandomizedSet();// Inserts 1 to the set. Returns true as 1 was inserted successfully.randomSet.insert(1);// Returns false as 2 does not exist in the set.randomSet.remove(2);// Inserts 2 to the set, returns true. Set now contains [1,2].randomSet.insert(2);// getRandom should return either 1 or 2 randomly.randomSet.getRandom();// Removes 1 from the set, returns true. Set now contains [2].randomSet.remove(1);// 2 was already in the set, so return false.randomSet.insert(2);// Since 2 is the only number in the set, getRandom always return 2.randomSet.getRandom();

剛開(kāi)始想的是用set,但用到getRandom()時(shí),會(huì)用到循環(huán),時(shí)間不是O(n)

class RandomizedSet {    set<int> nums;public:    /** Initialize your data structure here. */    RandomizedSet() {            }        /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */    bool insert(int val) {        if(nums.find(val)!=nums.end()) return 0;       nums.insert(val);       return 1;    }        /** Removes a value from the set. Returns true if the set contained the specified element. */    bool remove(int val) {      if(nums.find(val)==nums.end())return 0;       nums.erase(val);       return 1;    }        /** Get a random element from the set. */    int getRandom() {       int rnd=rand()%nums.size(),i=0;       set<int>::iterator it;       for( it=nums.begin();i!=rnd;it++,i++);//這里用循環(huán)了,會(huì)慢       return *it;    }};轉(zhuǎn)載別人的:點(diǎn)擊打開(kāi)鏈接

用一個(gè)hash表記錄每個(gè)元素的值在vector中的位置,當(dāng)要?jiǎng)h除一個(gè)元素時(shí),先找到該元素的位置,將其更新為末尾元素,vector末尾彈出,同時(shí)更新hash表。

class RandomizedSet {    //set<int> nums;    vector<int> nums;    map<int ,int> locs;//vals to keypublic:    /** Initialize your data structure here. */    RandomizedSet() {            }        /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */    bool insert(int val) {      /*  if(nums.find(val)!=nums.end()) return 0;       nums.insert(val);       return 1;*/       if(locs.count(val)!=0) return 0;       nums.push_back(val);       locs[val]=(nums.size()-1);       return 1;    }        /** Removes a value from the set. Returns true if the set contained the specified element. */    bool remove(int val) {     /* if(nums.find(val)==nums.end())return 0;       nums.erase(val);       return 1;*/       if(locs.count(val)==0) return 0;             int last=nums[nums.size()-1];       int loc=locs[val];       nums[loc]=last;//更新nums       locs[last]=loc;//更新locs       locs.erase(val);       nums.pop_back();       return 1;    }        /** Get a random element from the set. */    int getRandom() {      /* int rnd=rand()%nums.size(),i=0;       set<int>::iterator it;       for( it=nums.begin();i!=rnd;it++,i++);       return *it;*/              return nums[rand()%nums.size()];    }};


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 吉首市| 宜昌市| 平顺县| 双江| 长子县| 赤城县| 专栏| 油尖旺区| 津市市| 铅山县| 东平县| 滕州市| 无为县| 松原市| 伊金霍洛旗| 武安市| 兴业县| 郯城县| 师宗县| 巢湖市| 淅川县| 宿松县| 沂南县| 安吉县| 石狮市| 朝阳区| 绩溪县| 宜丰县| 稻城县| 桓台县| 徐水县| 武陟县| 九台市| 周宁县| 广西| 浪卡子县| 双鸭山市| 信宜市| 南雄市| 汤阴县| 临沭县|