The test paper includes 4 questions, 3 programming questions and 1 question and answer, which is limited to 100 minutes.
First programming question
Title: Niuniu has a string s with a length of n only composed of characters' 1 'to' 9 '. Now Niuniu can intercept a substring with a length of k as a positive decimal integer. For example, for the substring "123", the corresponding decimal number is 123. Niuniu wants this positive integer to be as large as possible. Please help Niuniu calculate the positive integer. The function passes in a string s of length N and a positive integer k. please return the answer.
Note: it mainly investigates the conversion between string and decimal number.
class Solution { public: int maxValue(string s, int k) { int n = s.size(), ans = 0; for (int i = 0; i < n - k; ++i) { int t = 0; for (int j = 0; j < k; ++j) { t = t * 10 + (s[i + j] - '0'); } ans = max(ans, t); } return ans; } };
The second programming problem
Title: Niuniu has a binary tree with n nodes, and its root node is root. Niuniu wants to prune the leaf node of the current binary tree, but Niuniu cannot delete the leaf node directly. He can only trim the parent node of the leaf node. After trimming the parent node, the leaf node will be deleted accordingly. Niuniu wants to trim all leaf nodes on the premise of leaving as many nodes as possible. Please return to the pruned binary tree.
Note: this title can't be written. It was reproduced later by referring to the Python code of a big man. The first value of pair indicates whether the node is a leaf node and the second value indicates whether the node should be deleted.
class Solution { private: pair<bool, bool> dfs(TreeNode* root) { if (!root) { return { false, false }; } pair<bool, bool> lp, rp; if (root->left) { lp = dfs(root->left); } if (root->right) { rp = dfs(root->right); } if (lp.first || rp.first) { return { false, true }; } if (lp.second) { root->left = nullptr; } if (rp.second) { root->right = nullptr; } return { true, false }; } public: TreeNode* pruneLeaves(TreeNode* root) { TreeNode* t = new TreeNode(-1); t->left = root; dfs(t); root = t->left; delete t; t = nullptr; return root; } };
The third programming problem
Title: Niu Mei gave Niu Niu a positive integer array a with a subscript of length n. careless Niu Niu accidentally deleted some of the numbers. If the added ai is deleted, then ai=0. For all deleted numbers, Niu Niu selects a positive integer and fills it in. Now Niuniu wants to know how many filling schemes there are so that a0 ≤ a1 ≤... ≤ an-1 and 1 ≤ ai ≤ k are satisfied for all 0 ≤ i ≤ n-1. The function passes in an array a with a subscript starting from 0 and a positive integer k. please return the modulus of 10 ^ 9 + 7 of the legal number of filling schemes to ensure that there is no data with a number of schemes of 0.
Note: I know that this problem should be solved by dynamic programming, but I can't write it. The following problem solution comes from the same Python boss. I reproduced it in C + +.
class Solution { private: int dfs(int p, int q, map<pair<int, int>, int>& map) { if (map.count({ p, q })) { return map[{p, q}]; } if (p == 0 || q == 0) { return 1; } if (p == 1) { return q; } if (q == 1) { return 1; } map[{p, q}] = (dfs(p - 1, q, map) + dfs(p, q - 1, map)) % ((int)pow(10, 9) + 7); return map[{p, q}]; } public: int fillArray(vector<int>& a, int k) { map<pair<int, int>, int> map; a.insert(a.begin(), 1); a.emplace_back(max(a[a.size() - 1], k)); int idx = 0, ans = 1; for (int i = 1; i < a.size(); ++i) { if (a[i] != 0) { ans = ans * dfs(i - idx - 1, a[i] - a[idx] + 1, map) % ((int)pow(10, 9) + 7); idx = i; } } return ans; } };
I am just a programming Xiaobai. I write a blog to summarize and communicate. Please criticize and correct!