added randomizedset
This commit is contained in:
@@ -15,3 +15,4 @@ add_subdirectory(maxprofit2)
|
|||||||
add_subdirectory(canJump)
|
add_subdirectory(canJump)
|
||||||
add_subdirectory(jump)
|
add_subdirectory(jump)
|
||||||
add_subdirectory(hindex)
|
add_subdirectory(hindex)
|
||||||
|
add_subdirectory(randomizedset)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
SUBDIRS := canJump jump majorityelement maxprofit maxprofit2 rmdupfromsortedarray rmdupfromsortedarray2 rotate twopointers hindex
|
SUBDIRS := canJump jump majorityelement maxprofit maxprofit2 rmdupfromsortedarray rmdupfromsortedarray2 rotate twopointers hindex randomizedset
|
||||||
|
|
||||||
all: $(SUBDIRS)
|
all: $(SUBDIRS)
|
||||||
|
|
||||||
|
|||||||
@@ -9,3 +9,4 @@ subdir('maxprofit2')
|
|||||||
subdir('canJump')
|
subdir('canJump')
|
||||||
subdir('jump')
|
subdir('jump')
|
||||||
subdir('hindex')
|
subdir('hindex')
|
||||||
|
subdir('randomizedset')
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
FILE(GLOB_RECURSE SOURCES src/*.cpp)
|
||||||
|
|
||||||
|
add_executable(
|
||||||
|
randomizedset
|
||||||
|
${SOURCES}
|
||||||
|
)
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
CXX = g++
|
||||||
|
CFLAGS = -Wall -g
|
||||||
|
SRCS = $(wildcard src/*/*.cpp src/*.cpp)
|
||||||
|
OBJS = $(SRCS:.cpp=.o)
|
||||||
|
TARGET = randomizedset
|
||||||
|
CROSS_COMPILE =
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(OBJS)
|
||||||
|
$(CROSS_COMPILE)$(CXX) $(LDFLAGS) $^ -o $@ $(LIBS)
|
||||||
|
|
||||||
|
%.o: %.cpp
|
||||||
|
$(CROSS_COMPILE)$(CXX) $(CFLAGS) $(INCLUDES) -c $< -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(OBJS) $(TARGET)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
executable('randomizedset', 'src/main.cpp', install: true)
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <iostream>
|
||||||
|
#include "randomizedset.hpp"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
RandomizedSet *randomizedSet = new RandomizedSet();
|
||||||
|
bool res = randomizedSet->insert(1);
|
||||||
|
std::cout << res << std::endl;
|
||||||
|
assert(res == true);
|
||||||
|
|
||||||
|
res = randomizedSet->remove(2);
|
||||||
|
std::cout << res << std::endl;
|
||||||
|
assert(res == false);
|
||||||
|
|
||||||
|
res = randomizedSet->insert(2);
|
||||||
|
std::cout << res << std::endl;
|
||||||
|
assert(res == true);
|
||||||
|
|
||||||
|
int ires = randomizedSet->getRandom();
|
||||||
|
std::cout << ires << std::endl; // Функция getRandom() должна возвращать
|
||||||
|
// случайным образом либо 1, либо 2.
|
||||||
|
|
||||||
|
res = randomizedSet->remove(1);
|
||||||
|
std::cout << res << std::endl;
|
||||||
|
assert(res == true);
|
||||||
|
|
||||||
|
res = randomizedSet->insert(2);
|
||||||
|
std::cout << res << std::endl;
|
||||||
|
assert(res == false);
|
||||||
|
|
||||||
|
ires = randomizedSet->getRandom();
|
||||||
|
std::cout << ires << std::endl;
|
||||||
|
assert(ires == 2);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#include <cstdlib>
|
||||||
|
#include <ctime>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class RandomizedSet {
|
||||||
|
public:
|
||||||
|
RandomizedSet() {
|
||||||
|
srand(time(nullptr));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool insert(int val) {
|
||||||
|
if (map.find(val) != map.end()) return false;
|
||||||
|
map[val] = list.size();
|
||||||
|
list.push_back(val);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool remove(int val) {
|
||||||
|
auto it = map.find(val);
|
||||||
|
if (it == map.end()) return false;
|
||||||
|
int idx = it->second;
|
||||||
|
int lastVal = list.back();
|
||||||
|
list[idx] = lastVal;
|
||||||
|
map[lastVal] = idx;
|
||||||
|
list.pop_back();
|
||||||
|
map.erase(it);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getRandom() {
|
||||||
|
return list[rand() % list.size()];
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<int> list;
|
||||||
|
std::unordered_map<int, int> map;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user