# Leetcode game 270 weekly question solution

## Problem A - Find the 3-bit even number

meaning of the title
Any three numbers in the array are spliced into three digits. Non even numbers and no leading 0 are required. All qualified numbers are returned in ascending order
thinking
See that the range of the given array is 100, and directly triple loop. Since the requirement is not repeated, it is processed with set.
code

class Solution {
public:
vector<int> findEvenNumbers(vector<int>& digits) {
int n=digits.size();
set<int> ans;
vector<int> res;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
if(i!=j&&i!=k&&j!=k){
int x=digits[i]*100+digits[j]*10+digits[k];
if(x>=100&&x%2==0) ans.insert(x);
}
}
}
}
for(auto i:ans) res.push_back(i);
return res;
}
};


## Problem B - Delete the intermediate node of the linked list

Meaning:
Give you a head node of the linked list. Delete the intermediate node of the linked list
Idea:
First traverse the length len, and then skip ⌊ len/2 ⌋
code

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode() : val(0), next(nullptr) {}
*     ListNode(int x) : val(x), next(nullptr) {}
*     ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
int length = 0;
++length;
}
return length;
}
ListNode* dummy = new ListNode(0, head);
ListNode* cur = dummy;
for (int i = 0; i < len/2; ++i) cur = cur->next;
cur->next = cur->next->next;
ListNode* ans = dummy->next;
delete dummy;
return ans;
}
};


## Problem C - The direction of each step from one node to another in a binary tree

Meaning:
Given a binary tree, find the path from the starting point to the end point (up is U, left is L, right is R).
Idea:
First of all, we can easily think of the nearest common ancestor (LCA). Combined with the skills used to calculate the distance between two points on the tree (the sum of the distance to the root node - 2 * (the distance from LCA to the root)), we can think of:

1. Calculate the path (dfs) from the root node to the start and end points respectively
2. Remove the common prefix of the two paths (the path to the LCA) to obtain the paths from the LCA to the starting point and the ending point respectively
3. Find str1: the process of running from the starting point to LCA is replaced by 'U'
4. Find STR2: the process of LCA running to the end point is the path from the root node to the end point - the path from the root node to LCA. There is no need to deal with it after removing the prefix
So str1+str2 above is the answer
code
/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
string st="",ed="",tmp="";
void dfs(TreeNode* now,int to1,int to2){
if(now==nullptr) return;
if(now->val==to1) st=tmp;
if(now->val==to2) ed=tmp;
if(now->left!=nullptr){
tmp+="L";
dfs(now->left,to1,to2);
tmp.pop_back();
}
if(now->right!=nullptr){
tmp+="R";
dfs(now->right,to1,to2);
tmp.pop_back();
}
}
string getDirections(TreeNode* root, int startValue, int destValue) {
if(root==nullptr) return tmp;
dfs(root,startValue,destValue);
int ind=0;
while(ind<min(st.size(),ed.size())&&st[ind]==ed[ind]) ind++;
string ans;
for(int i=ind;i<st.size();i++) ans+="U";
for(int i=ind;i<ed.size();i++) ans+=ed[i];
return ans;
}
};


## Problem D - Legal rearrangement of number pairs

meaning of the title
Give you n tuples and rearrange them so that the second element of each tuple is equal to the first element of the next tuple
thinking
We treat each binary [u,v] as a directed edge from u to V, then the problem is transformed into an Euler path of a classical directed graph. Since the solution is guaranteed, the graph given by the problem must meet one of the following two conditions:

• All point in degrees are equal to out degrees;
• There is exactly one point in degree = in degree + 1 (the starting point of Euler path), and there is exactly one point in degree = out degree + 1 (the end point of Euler path), and the other points in degree is equal to out degree.

That is, the corresponding graph of this problem must be Euler / semi Euler graph. For the first one, choose any point to start dfs; For the second, select the starting point to start dfs. Complexity O ( m ) \mathcal{O}(m) O(m).
Note that the complexity is reduced by deleting edges from the back, because this can make each edge accessed only once, so as to achieve O ( m ) \mathcal{O}(m) Complexity of O(m).
code

class Solution {
map<int, vector<int>> mp;
map<int, int> deg;
vector<vector<int>> ans;
void dfs(int sn) {
vector<int> &e = mp[sn];
while (!e.empty()) {
int fn = e.back();
e.pop_back();
dfs(fn);
ans.push_back(vector<int>{sn, fn});
}
}
public:
vector<vector<int>> validArrangement(vector<vector<int>>& pairs) {
for (auto &pair : pairs) {
mp[pair[0]].push_back(pair[1]);
deg[pair[0]]--, deg[pair[1]]++;
}
for (auto i:deg) if (i.second == -1) dfs(i.first);
if (ans.empty()) dfs(deg.begin()->first);
reverse(ans.begin(), ans.end());
return ans;
}
};


Posted by derekbelcher on Sat, 04 Dec 2021 22:51:46 -0800