leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 1911873b6e9970e7cb068bc3f6fc6c16037cb1de
parent 679b78df51a2daa3caf1c6a7c0cf1adede127c22
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri, 10 Mar 2023 20:53:36 +0100

Daily Problem

Diffstat:
AProblems/0382.cpp | 16++++++++++++++++
MREADME.md | 1+
2 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/Problems/0382.cpp b/Problems/0382.cpp @@ -0,0 +1,16 @@ +class Solution { + ListNode *head = nullptr; + int size = 0; + +public: + Solution(ListNode *head) : head(head) { + for (ListNode *p = head; p; p = p->next) size++; + } + + int getRandom() { + int elem = rand() % size; + ListNode *p = head; + while (elem--) p = p->next; + return p->val; + } +}; diff --git a/README.md b/README.md @@ -215,6 +215,7 @@ for solving problems. | 0374 | Easy | [Guess Number Higher or Lower](Problems/0374.cpp) | | 0376 | Medium | [Wiggle Subsequence](Problems/0376.cpp) | | 0377 | Medium | [Combination Sum IV](Problems/0377.cpp) | +| 0382 | Medium | [Linked List Random Node](Problems/0382.cpp) | | 0383 | Easy | [Ransom Note](Problems/0383.cpp) | | 0384 | Medium | [Shuffle an Array](Problems/0384.cpp) | | 0387 | Easy | [First Unique Character in a String](Problems/0387.cpp) |