From ba2e393b13f177e14533aac2b0c5ea9b10adf0b4 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Sun, 10 Apr 2022 13:53:15 +0800 Subject: [PATCH] add: desc [cpp] --- cpp/2204/220403-CN.cpp | 7 +++++++ cpp/2204/220403.cpp | 6 ++++++ cpp/2204/220404.cpp | 6 ++++++ cpp/2204/220405-CN.cpp | 7 +++++++ cpp/2204/220406-CN.cpp | 8 ++++++++ cpp/2204/220406.cpp | 6 ++++++ cpp/2204/220407-CN.cpp | 7 +++++++ cpp/2204/220407.cpp | 13 +++++++++++++ cpp/2204/220408-CN.cpp | 5 +++++ cpp/2204/220408.cpp | 8 ++++++++ cpp/2204/220409-CN.cpp | 10 ++++++++-- cpp/2204/220409.cpp | 12 ++++++++++++ cpp/2204/220410-CN.cpp | 16 ++++++++++++++++ 13 files changed, 109 insertions(+), 2 deletions(-) diff --git a/cpp/2204/220403-CN.cpp b/cpp/2204/220403-CN.cpp index ff04f54..8991aa0 100644 --- a/cpp/2204/220403-CN.cpp +++ b/cpp/2204/220403-CN.cpp @@ -3,6 +3,13 @@ #include #include +/** + * 744. Find Smallest Letter Greater Than Target + * Given a characters array letters that is sorted in non-decreasing order and a character target, return the smallest character in the array that is larger than target. + * Note that the letters wrap around. + * For example, if target == 'z' and letters == ['a', 'b'], the answer is 'a'. + */ + class Solution { public: static char nextGreatestLetter(const std::vector& letters, char target) { diff --git a/cpp/2204/220403.cpp b/cpp/2204/220403.cpp index 11fa75f..eca76c9 100644 --- a/cpp/2204/220403.cpp +++ b/cpp/2204/220403.cpp @@ -1,6 +1,12 @@ #include #include +/** + * 31. Next Permutation + * + * Description too long, but it was 100% what the STL implements. + */ + class Solution { public: static inline void nextPermutation(std::vector& nums) { diff --git a/cpp/2204/220404.cpp b/cpp/2204/220404.cpp index 7c7c5f6..9c95234 100644 --- a/cpp/2204/220404.cpp +++ b/cpp/2204/220404.cpp @@ -10,6 +10,12 @@ struct ListNode { ~ListNode() { delete this->next; this->next = nullptr; } }; +/** + * 1721. Swapping Nodes in a Linked List + * You are given the head of a linked list, and an integer k. + * Return the head of the linked list after swapping the values of the kth node from the beginning and the kth node from the end (the list is 1-indexed). + */ + class Solution { public: static ListNode* swapNodes(ListNode* head, int k) { diff --git a/cpp/2204/220405-CN.cpp b/cpp/2204/220405-CN.cpp index c9a982a..18dcfda 100644 --- a/cpp/2204/220405-CN.cpp +++ b/cpp/2204/220405-CN.cpp @@ -1,5 +1,12 @@ #include +/** + * 762. Prime Number of Set Bits in Binary Representation + * Given two integers left and right, return the count of numbers in the inclusive range [left, right] having a prime number of set bits in their binary representation. + * Recall that the number of set bits an integer has is the number of 1's present when written in binary. + * For example, 21 written in binary is 10101, which has 3 set bits. + */ + class Solution { private: static inline const int IS_PRIME[32] = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1}; diff --git a/cpp/2204/220406-CN.cpp b/cpp/2204/220406-CN.cpp index aae0d0c..eee744c 100644 --- a/cpp/2204/220406-CN.cpp +++ b/cpp/2204/220406-CN.cpp @@ -2,6 +2,14 @@ #include #include +/** + * 310. Minimum Height Trees + * A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree. + * Given a tree of n nodes labelled from 0 to n - 1, and an array of n - 1 edges where edges[i] = [ai, bi] indicates that there is an undirected edge between the two nodes ai and bi in the tree, you can choose any node of the tree as the root. When you select a node x as the root, the result tree has height h. Among all possible rooted trees, those with minimum height (i.e. min(h)) are called minimum height trees (MHTs). + * Return a list of all MHTs' root labels. You can return the answer in any order. + * The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf. + */ + class Solution { public: static std::vector findMinHeightTrees(int n, const std::vector>& edges) { diff --git a/cpp/2204/220406.cpp b/cpp/2204/220406.cpp index d5de496..7e07c85 100644 --- a/cpp/2204/220406.cpp +++ b/cpp/2204/220406.cpp @@ -1,6 +1,12 @@ #include #include +/** + * 923. 3Sum With Multiplicity + * Given an integer array arr, and an integer target, return the number of tuples i, j, k such that i < j < k and arr[i] + arr[j] + arr[k] == target. + * As the answer can be very large, return it modulo 109 + 7. + */ + class Solution { private: static inline const int MAX_VALUE = 100 + 2; diff --git a/cpp/2204/220407-CN.cpp b/cpp/2204/220407-CN.cpp index 78a6f1b..987791b 100644 --- a/cpp/2204/220407-CN.cpp +++ b/cpp/2204/220407-CN.cpp @@ -1,5 +1,12 @@ #include +/** + * 796. Rotate String + * Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s. + * A shift on s consists of moving the leftmost character of s to the rightmost position. + * For example, if s = "abcde", then it will be "bcdea" after one shift. + */ + class Solution { public: static bool rotateString(const std::string& s, const std::string& g) { diff --git a/cpp/2204/220407.cpp b/cpp/2204/220407.cpp index eb8eb07..7b933ef 100644 --- a/cpp/2204/220407.cpp +++ b/cpp/2204/220407.cpp @@ -2,6 +2,19 @@ #include #include +/** + * 1046. Last Stone Weight + * You are given an array of integers stones where stones[i] is the weight of the ith stone. + * + * We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights x and y with x <= y. The result of this smash is: + * + * If x == y, both stones are destroyed, and + * If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x. + * At the end of the game, there is at most one stone left. + * + * Return the smallest possible weight of the left stone. If there are no stones left, return 0. + */ + class Solution { public: static int lastStoneWeight(const std::vector& stones) { diff --git a/cpp/2204/220408-CN.cpp b/cpp/2204/220408-CN.cpp index 84b50ae..d8be9b3 100644 --- a/cpp/2204/220408-CN.cpp +++ b/cpp/2204/220408-CN.cpp @@ -12,6 +12,11 @@ public: explicit Node(int _val = 0, std::vector _children = {}) : val(_val), children(std::move(_children)) {} }; +/** + * 429. N-ary Tree Level Order Traversal + * Given an n-ary tree, return the level order traversal of its nodes' values. + * Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples). + */ class Solution { public: diff --git a/cpp/2204/220408.cpp b/cpp/2204/220408.cpp index 0ae6aee..f0ddd05 100644 --- a/cpp/2204/220408.cpp +++ b/cpp/2204/220408.cpp @@ -1,6 +1,14 @@ #include #include +/** + * 703. Kth Largest Element in a Stream + * Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element. + * Implement KthLargest class: + * KthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums. + * int add(int val) Appends the integer val to the stream and returns the element representing the kth largest element in the stream. + */ + class KthLargest { private: int k; diff --git a/cpp/2204/220409-CN.cpp b/cpp/2204/220409-CN.cpp index 7c7291f..cb19711 100644 --- a/cpp/2204/220409-CN.cpp +++ b/cpp/2204/220409-CN.cpp @@ -2,13 +2,19 @@ #include #include +/** + * 780. Reaching Points + * Given four integers sx, sy, tx, and ty, return true if it is possible to convert the point (sx, sy) to the point (tx, ty) through some operations, or false otherwise. + * The allowed operation on some point (x, y) is to convert it to either (x, x + y) or (x + y, y). + */ + class Solution { public: - static bool reachingPoints(int sx, int sy, int tx, int ty) { + static constexpr bool reachingPoints(int sx, int sy, int tx, int ty) { for (; tx != ty && tx > sx && ty > sy; *(tx > ty ? &tx : &ty) = std::abs(tx - ty)) if (tx == sx && ty == sy) return true; - return (tx == sx && ty > sy && !((ty - sy) % sx)) || (ty == sy && tx > sx && !((tx - sx) % sy)); + return (tx == sx && ty > sy && !((ty - sy) % sx)) || (ty == sy && tx > sx && !((tx - sx) % sy)) || (tx == sx && ty == sy); } }; diff --git a/cpp/2204/220409.cpp b/cpp/2204/220409.cpp index be36a45..1274926 100644 --- a/cpp/2204/220409.cpp +++ b/cpp/2204/220409.cpp @@ -3,6 +3,18 @@ #include #include +/** + * 347. Top K Frequent Elements + * Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. + * + * Complexity analysis: + * Solution: O(n + k) + * Solution2: O(n + n * log(k)) + * Solution3: O(n + k * log(n)) + * + * But "Solution" is the slowest. + */ + class Solution { public: static std::vector topKFrequent(const std::vector& nums, int k) { diff --git a/cpp/2204/220410-CN.cpp b/cpp/2204/220410-CN.cpp index e5716a6..f93b651 100644 --- a/cpp/2204/220410-CN.cpp +++ b/cpp/2204/220410-CN.cpp @@ -2,6 +2,22 @@ #include #include +/** + * 804. Unique Morse Code Words + * International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: + * + * 'a' maps to ".-", + * 'b' maps to "-...", + * 'c' maps to "-.-.", and so on. + * For convenience, the full table for the 26 letters of the English alphabet is given below: + * + * [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] + * Given an array of strings words where each word can be written as a concatenation of the Morse code of each letter. + * + * For example, "cab" can be written as "-.-..--...", which is the concatenation of "-.-.", ".-", and "-...". We will call such a concatenation the transformation of a word. + * Return the number of different transformations among all words we have. + */ + class Solution { private: inline static const std::string MORSE[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};