diff options
Diffstat (limited to 'insert_delete_getrandom.cpp')
-rw-r--r-- | insert_delete_getrandom.cpp | 45 |
1 files changed, 0 insertions, 45 deletions
diff --git a/insert_delete_getrandom.cpp b/insert_delete_getrandom.cpp deleted file mode 100644 index 5bb76da..0000000 --- a/insert_delete_getrandom.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "leetcode.h" - -class RandomizedSet { -public: - bool insert(int val) { - if (data.count(val)) - return false; - data[val] = idxs.size(); - idxs.push_back(val); - return true; - } - bool remove(int val) { - if (!data.count(val)) - return false; - int lst = idxs.back(), pos = data[val]; - data[lst] = pos; - idxs[pos] = lst; - data.erase(val); - idxs.pop_back(); - return true; - } - int getRandom() { return idxs[rand() % idxs.size()]; } - -private: - unordered_map<int, int> data; - vector<int> idxs; -}; - -int main() { - RandomizedSet *obj = new RandomizedSet(); - bool param_1 = obj->insert(2); - bool param_2 = obj->remove(1); - int param_3 = obj->getRandom(); - cout << "param_1 (insert 2): "; - if (param_1) - cout << "true\n"; - else - cout << "false\n"; - cout << "param_2 (remove 1): "; - if (param_2) - cout << "true\n"; - else - cout << "false\n"; - cout << "param_3 (getRandom): " << param_3; -} |