国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 淅川县| 孝感市| 盘山县| 瓮安县| 调兵山市| 马公市| 蒙山县| 平利县| 阿合奇县| 黔东| 东莞市| 巩留县| 绥阳县| 麻江县| 涟源市| 富裕县| 四会市| 衡山县| 广宗县| 瓮安县| 遂宁市| 土默特左旗| 遂川县| 华阴市| 巴林右旗| 邓州市| 济宁市| 永福县| 张掖市| 介休市| 大石桥市| 花莲县| 延边| 来宾市| 舟山市| 柳林县| 云林县| 抚松县| 郓城县| 太原市| 玉林市|