Insert Delete GetRandom O(1) Solutions in C++
Number 380
Difficulty Medium
Acceptance 47.6%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/insert-delete-getrandom-o1/// Author : Hao Chen// Date : 2016-08-25class RandomizedSet {public:/** Initialize your data structure here. */RandomizedSet() {srand(time(NULL));}/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */bool insert(int val) {if ( find(val) ) return false;data.push_back(val);valpos[val] = data.size() - 1;return true;}/** Removes a value from the set. Returns true if the set contained the specified element. */bool remove(int val) {if ( !find(val) ) return false;/** Tricky* ------* 1) Copy the data from the last one to the place need be removed.* 2) Remove the last one.*/int _idx = valpos[val];int _val = data.back();data[_idx] = _val;valpos[_val] = _idx;valpos.erase(val);data.pop_back();return true;}/** Get a random element from the set. */int getRandom() {return data[ rand() % data.size() ];}private:unordered_map<int, int> valpos; //value position mapvector<int> data;bool find(int val) {return (valpos.find(val) != valpos.end());}};/*** Your RandomizedSet object will be instantiated and called as such:* RandomizedSet obj = new RandomizedSet();* bool param_1 = obj.insert(val);* bool param_2 = obj.remove(val);* int param_3 = obj.getRandom();*/
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/insert-delete-getrandom-o1/description//// Author : liuyubobobo/// Time : 2018-07-08#include <iostream>#include <vector>#include <unordered_map>using namespace std;/// Vector to stroe all elements/// HashMap to restore all maps between elements and index/// Time Complexity: O(1)/// Space Complexity: O(n)class RandomizedSet {private:vector<int> nums;unordered_map<int, int> index;public:/** Initialize your data structure here. */RandomizedSet() {nums.clear();index.clear();}/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */bool insert(int val) {if(index.find(val) == index.end()){nums.push_back(val);index[val] = nums.size() - 1;return true;}return false;}/** Removes a value from the set. Returns true if the set contained the specified element. */bool remove(int val) {if(index.find(val) != index.end()){int i = index[val];index.erase(val);int num = nums.back();nums.pop_back();if(num != val){nums[i] = num;index[num] = i;}return true;}return false;}/** Get a random element from the set. */int getRandom() {int rndIndex = rand() % nums.size();return nums[rndIndex];}void printInfo(){cout << "nums : ";for(int num: nums)cout << num << " ";cout << endl;cout << "index : ";for(const pair<int, int>& p: index)cout << "( " << p.first << " , " << p.second << " ) ";cout << endl;}};void print_bool(bool res){cout << (res ? "True" : "False") << endl;}int main() {RandomizedSet randomSet;print_bool(randomSet.insert(0));randomSet.printInfo();print_bool(randomSet.remove(0));randomSet.printInfo();print_bool(randomSet.insert(-1));randomSet.printInfo();print_bool(randomSet.remove(0));randomSet.printInfo();for(int i = 0 ; i < 10 ; i ++)cout << randomSet.getRandom() << " ";cout << endl;return 0;}