add: 220517 [cpp]

This commit is contained in:
Eat-Swap 2022-05-18 00:27:09 +08:00
parent c0848960a6
commit 6e4962af86
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
3 changed files with 37 additions and 1 deletions

View File

@ -2,6 +2,12 @@
#include <string>
#include <cstdio>
/**
* 953. Verifying an Alien Dictionary
* In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.
* Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographically in this alien language.
*/
class Solution {
public:
static bool isAlienSorted(const std::vector<std::string>& words, const std::string& order) {

30
cpp/2205/220517.cpp Normal file
View File

@ -0,0 +1,30 @@
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
explicit TreeNode(int x = 0, TreeNode* l = nullptr, TreeNode* r = nullptr) : val(x), left(l), right(r) {}
};
/**
* 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree
* Given two binary trees original and cloned and given a reference to a node target in the original tree.
* The cloned tree is a copy of the original tree.
* Return a reference to the same node in the cloned tree.
* Note that you are not allowed to change any of the two trees or the target node and the answer must be a reference to a node in the cloned tree.
*/
class Solution {
private:
template<typename T>
static T* notNull(T* p1, T* p2) {
return p1 ? p1 : p2;
}
public:
static TreeNode* getTargetCopy(TreeNode* original, TreeNode* cloned, TreeNode* target) {
if (!original || !cloned || !target)
return nullptr;
return original == target ? cloned : notNull(getTargetCopy(original->left, cloned->left, target), getTargetCopy(original->right, cloned->right, target));
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2205 220517-CN.cpp)
ADD_EXECUTABLE(2205 220517-CN.cpp 220517.cpp)