add: bulk descriptions [cpp]
This commit is contained in:
parent
e8e1a2b8b6
commit
0cb81a8149
|
|
@ -2,6 +2,13 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 798. Smallest Rotation with Highest Score
|
||||||
|
* You are given an array nums. You can rotate it by a non-negative integer k so that the array becomes [nums[k], nums[k + 1], ... nums[nums.length - 1], nums[0], nums[1], ..., nums[k-1]]. Afterward, any entries that are less than or equal to their index are worth one point.
|
||||||
|
* - For example, if we have nums = [2,4,1,3,0], and we rotate by k = 2, it becomes [1,3,0,2,4]. This is worth 3 points because 1 > 0 [no points], 3 > 1 [no points], 0 <= 2 [one point], 2 <= 3 [one point], 4 <= 4 [one point].
|
||||||
|
* Return the rotation index k that corresponds to the highest score we can achieve if we rotated nums by it. If there are multiple answers, return the smallest such index k.
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static int bestRotation(const std::vector<int>& nums) {
|
static int bestRotation(const std::vector<int>& nums) {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,12 @@ struct ListNode {
|
||||||
explicit ListNode(int x = 0, ListNode* next = nullptr) : val(x), next(next) {}
|
explicit ListNode(int x = 0, ListNode* next = nullptr) : val(x), next(next) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 2. Add Two Numbers
|
||||||
|
* You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
|
||||||
|
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
|
static ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,13 @@
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 2049. Count Nodes With the Highest Score
|
||||||
|
* There is a binary tree rooted at 0 consisting of n nodes. The nodes are labeled from 0 to n - 1. You are given a 0-indexed integer array parents representing the tree, where parents[i] is the parent of node i. Since node 0 is the root, parents[0] == -1.
|
||||||
|
* Each node has a score. To find the score of a node, consider if the node and the edges connected to it were removed. The tree would become one or more non-empty subtrees. The size of a subtree is the number of the nodes in it. The score of the node is the product of the sizes of all those subtrees.
|
||||||
|
* Return the number of nodes that have the highest score.
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static int countHighestScoreNodes(const std::vector<int>& parents) {
|
static int countHighestScoreNodes(const std::vector<int>& parents) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,15 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 393. UTF-8 Validation
|
||||||
|
* Given an integer array data representing the data, return whether it is a valid UTF-8 encoding.
|
||||||
|
* A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:
|
||||||
|
* - For a 1-byte character, the first bit is a 0, followed by its Unicode code.
|
||||||
|
* - For an n-bytes character, the first n bits are all one's, the n + 1 bit is 0, followed by n - 1 bytes with the most significant 2 bits being 10.
|
||||||
|
* Note: The input is an array of integers. Only the least significant 8 bits of each integer is used to store the data. This means each integer represents only 1 byte of data.
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static bool validUtf8(const std::vector<int>& data) {
|
static bool validUtf8(const std::vector<int>& data) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,14 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 20. Valid Parentheses
|
||||||
|
* Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
|
||||||
|
* An input string is valid if:
|
||||||
|
* 1. Open brackets must be closed by the same type of brackets.
|
||||||
|
* 2. Open brackets must be closed in the correct order.
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static bool isValid(const std::string& s) {
|
static bool isValid(const std::string& s) {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,12 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 599. Minimum Index Sum of Two Lists
|
||||||
|
* Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.
|
||||||
|
* You need to help them find out their common interest with the least list index sum. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static std::vector<std::string> findRestaurant(const std::vector<std::string>& list1, const std::vector<std::string>& list2) {
|
static std::vector<std::string> findRestaurant(const std::vector<std::string>& list1, const std::vector<std::string>& list2) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,12 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 2044. Count Number of Maximum Bitwise-OR Subsets
|
||||||
|
* Given an integer array nums, find the maximum possible bitwise OR of a subset of nums and return the number of different non-empty subsets with the maximum bitwise OR.
|
||||||
|
* An array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b. Two subsets are considered different if the indices of the elements chosen are different.
|
||||||
|
* The bitwise OR of an array a is equal to a[0] OR a[1] OR ... OR a[a.length - 1] (0-indexed).
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static int countMaxOrSubsets(const std::vector<int>& nums) {
|
static int countMaxOrSubsets(const std::vector<int>& nums) {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,16 @@
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1249. Minimum Remove to Make Valid Parentheses
|
||||||
|
* Given a string s of '(' , ')' and lowercase English characters.
|
||||||
|
* Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.
|
||||||
|
* Formally, a parentheses string is valid if and only if:
|
||||||
|
* - It is the empty string, contains only lowercase characters, or
|
||||||
|
* - It can be written as AB (A concatenated with B), where A and B are valid strings, or
|
||||||
|
* - It can be written as (A), where A is a valid string.
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static std::string minRemoveToMakeValid(const std::string& s) {
|
static std::string minRemoveToMakeValid(const std::string& s) {
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,17 @@
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 432. All O`one Data Structure
|
||||||
|
* Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.
|
||||||
|
* Implement the AllOne class:
|
||||||
|
* - AllOne() Initializes the object of the data structure.
|
||||||
|
* - inc(String key) Increments the count of the string key by 1. If key does not exist in the data structure, insert it with count 1.
|
||||||
|
* - dec(String key) Decrements the count of the string key by 1. If the count of key is 0 after the decrement, remove it from the data structure. It is guaranteed that key exists in the data structure before the decrement.
|
||||||
|
* - getMaxKey() Returns one of the keys with the maximal count. If no element exists, return an empty string "".
|
||||||
|
* - getMinKey() Returns one of the keys with the minimum count. If no element exists, return an empty string "".
|
||||||
|
*/
|
||||||
|
|
||||||
class AllOne {
|
class AllOne {
|
||||||
private:
|
private:
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,11 @@
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 946. Validate Stack Sequences
|
||||||
|
* Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise.
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static bool validateStackSequences(const std::vector<int>& pushed, const std::vector<int>& popped) {
|
static bool validateStackSequences(const std::vector<int>& pushed, const std::vector<int>& popped) {
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,12 @@
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 720. Longest Word in Dictionary
|
||||||
|
* Given an array of strings words representing an English Dictionary, return the longest word in words that can be built one character at a time by other words in words.
|
||||||
|
* If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string.
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static std::string longestWord(const std::vector<std::string>& words) {
|
static std::string longestWord(const std::vector<std::string>& words) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,15 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 856. Score of Parentheses
|
||||||
|
* Given a balanced parentheses string s, return the score of the string.
|
||||||
|
* The score of a balanced parentheses string is based on the following rule:
|
||||||
|
* - "()" has score 1.
|
||||||
|
* - AB has score A + B, where A and B are balanced parentheses strings.
|
||||||
|
* - (A) has score 2 * A, where A is a balanced parentheses string.
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static int scoreOfParentheses(const std::string& s) {
|
static int scoreOfParentheses(const std::string& s) {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,18 @@
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 895. Maximum Frequency Stack
|
||||||
|
* Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
|
||||||
|
*
|
||||||
|
* Implement the FreqStack class:
|
||||||
|
*
|
||||||
|
* - FreqStack() constructs an empty frequency stack.
|
||||||
|
* - void push(int val) pushes an integer val onto the top of the stack.
|
||||||
|
* - int pop() removes and returns the most frequent element in the stack.
|
||||||
|
* - If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.
|
||||||
|
*/
|
||||||
|
|
||||||
class FreqStack {
|
class FreqStack {
|
||||||
private:
|
private:
|
||||||
std::unordered_map<int, std::stack<int>> s;
|
std::unordered_map<int, std::stack<int>> s;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue