add: 220107
This commit is contained in:
parent
5ba6b214c2
commit
a4b2d5557f
|
|
@ -1,6 +1,21 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1614. Maximum Nesting Depth of the Parentheses
|
||||||
|
* A string is a valid parentheses string (denoted VPS) if it meets one of the following:
|
||||||
|
* It is an empty string "", or a single character not equal to "(" or ")",
|
||||||
|
* It can be written as AB (A concatenated with B), where A and B are VPS's, or
|
||||||
|
* It can be written as (A), where A is a VPS.
|
||||||
|
* We can similarly define the nesting depth depth(S) of any VPS S as follows:
|
||||||
|
* depth("") = 0
|
||||||
|
* depth(C) = 0, where C is a string with a single character not equal to "(" or ")".
|
||||||
|
* depth(A + B) = max(depth(A), depth(B)), where A and B are VPS's.
|
||||||
|
* depth("(" + A + ")") = 1 + depth(A), where A is a VPS.
|
||||||
|
* For example, "", "()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's.
|
||||||
|
* Given a VPS represented as string s, return the nesting depth of s.
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static int maxDepth(const std::string& s) {
|
static int maxDepth(const std::string& s) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
#include <random>
|
||||||
|
#include <iostream>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 integer array nums.
|
||||||
|
* 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 choosen.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Definition for singly-linked list.
|
||||||
|
*/
|
||||||
|
struct ListNode {
|
||||||
|
int val;
|
||||||
|
ListNode *next;
|
||||||
|
ListNode() : val(0), next(nullptr) {}
|
||||||
|
explicit ListNode(int x) : val(x), next(nullptr) {}
|
||||||
|
ListNode(int x, ListNode *next) : val(x), next(next) {}
|
||||||
|
~ListNode() { delete this->next; }
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
private:
|
||||||
|
inline static std::mt19937 r = std::mt19937(std::time(nullptr));
|
||||||
|
int nums[10005]{};
|
||||||
|
int n = 0;
|
||||||
|
public:
|
||||||
|
explicit Solution(ListNode* head) {
|
||||||
|
do {
|
||||||
|
nums[n++] = head->val;
|
||||||
|
} while (head = head->next);
|
||||||
|
}
|
||||||
|
|
||||||
|
int getRandom() {
|
||||||
|
return nums[r() % n];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Your Solution object will be instantiated and called as such:
|
||||||
|
* Solution* obj = new Solution(head);
|
||||||
|
* int param_1 = obj->getRandom();
|
||||||
|
*/
|
||||||
|
int main() {
|
||||||
|
auto* head = new ListNode(1, new ListNode(2, new ListNode(3)));
|
||||||
|
auto* instance = new Solution(head);
|
||||||
|
std::cout << instance->getRandom() << std::endl;
|
||||||
|
std::cout << instance->getRandom() << std::endl;
|
||||||
|
std::cout << instance->getRandom() << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2201)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2201 220106.cpp)
|
ADD_EXECUTABLE(2201 220107.cpp)
|
||||||
Loading…
Reference in New Issue