diff --git a/cpp/2303/230310.cpp b/cpp/2303/230310.cpp new file mode 100644 index 0000000..717e907 --- /dev/null +++ b/cpp/2303/230310.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +struct ListNode { + int val; + ListNode* next; + + explicit ListNode(int x = 0, ListNode* next = nullptr) : val(x), next(next) {} +}; + +ListNode* construct(int x) { + return new ListNode(x); +} + +template +ListNode* construct(int x, Ts... xs) { + return new ListNode(x, construct(xs...)); +} + +/** + * 382. Linked List Random Node + * Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. + * + * Implement the Solution class: + * + * Solution(ListNode head) Initializes the object with the head of the singly-linked list head. + * int getRandom() Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be chosen. + */ + +class Solution { +private: + std::vector s; + +public: + explicit Solution(ListNode* head) { + for (auto* ptr = head; ptr; ptr = ptr->next) + s.push_back(ptr->val); + std::srand(std::time(nullptr)); + } + + int getRandom() const { + int r = rand(); + for (; r >= RAND_MAX / s.size() * s.size(); r = rand()); + return s[r % s.size()]; + } +}; diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index 3e5f772..01982d2 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230303.cpp) +ADD_EXECUTABLE(2303 230310.cpp)