add: desc [cpp]
This commit is contained in:
parent
f91b8422cc
commit
ba2e393b13
|
|
@ -3,6 +3,13 @@
|
||||||
#include <typeinfo>
|
#include <typeinfo>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static char nextGreatestLetter(const std::vector<char>& letters, char target) {
|
static char nextGreatestLetter(const std::vector<char>& letters, char target) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,12 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 31. Next Permutation
|
||||||
|
*
|
||||||
|
* Description too long, but it was 100% what the STL implements.
|
||||||
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static inline void nextPermutation(std::vector<int>& nums) {
|
static inline void nextPermutation(std::vector<int>& nums) {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,12 @@ struct ListNode {
|
||||||
~ListNode() { delete this->next; this->next = nullptr; }
|
~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 {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static ListNode* swapNodes(ListNode* head, int k) {
|
static ListNode* swapNodes(ListNode* head, int k) {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,12 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
class Solution {
|
||||||
private:
|
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};
|
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};
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,14 @@
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static std::vector<int> findMinHeightTrees(int n, const std::vector<std::vector<int>>& edges) {
|
static std::vector<int> findMinHeightTrees(int n, const std::vector<std::vector<int>>& edges) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,12 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
class Solution {
|
||||||
private:
|
private:
|
||||||
static inline const int MAX_VALUE = 100 + 2;
|
static inline const int MAX_VALUE = 100 + 2;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,12 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static bool rotateString(const std::string& s, const std::string& g) {
|
static bool rotateString(const std::string& s, const std::string& g) {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,19 @@
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static int lastStoneWeight(const std::vector<int>& stones) {
|
static int lastStoneWeight(const std::vector<int>& stones) {
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,11 @@ public:
|
||||||
explicit Node(int _val = 0, std::vector<Node*> _children = {}) : val(_val), children(std::move(_children)) {}
|
explicit Node(int _val = 0, std::vector<Node*> _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 {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,14 @@
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
class KthLargest {
|
||||||
private:
|
private:
|
||||||
int k;
|
int k;
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,19 @@
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
class Solution {
|
||||||
public:
|
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))
|
for (; tx != ty && tx > sx && ty > sy; *(tx > ty ? &tx : &ty) = std::abs(tx - ty))
|
||||||
if (tx == sx && ty == sy)
|
if (tx == sx && ty == sy)
|
||||||
return true;
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,18 @@
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static std::vector<int> topKFrequent(const std::vector<int>& nums, int k) {
|
static std::vector<int> topKFrequent(const std::vector<int>& nums, int k) {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,22 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
class Solution {
|
||||||
private:
|
private:
|
||||||
inline static const std::string MORSE[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
|
inline static const std::string MORSE[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue