Tencent Music Entertainment 2022 Campus Recruitment C++ Written Exam Programming Topics

Papers


The test paper includes 4 questions, 3 programming questions, and 1 quiz question, with a time limit of 100 minutes.


first programming question

Title: Niu Niu has a string s of length n consisting only of characters '1' to '9', now Niu Niu can intercept a substring of length k as a positive decimal integer, such as for the substring "123" , and its corresponding decimal number is 123. Niu Niu wants this positive integer to be as large as possible, please help Niu Niu to calculate the positive integer. The function passes in a string s of length n and a positive integer k, please return the answer.

Remarks: Mainly investigate the conversion between strings and decimal numbers.

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;
    }
};

Enter "321456987" and 3 to get the result 987.



second programming question

Title: Niu Niu has a binary tree with n nodes, and its root node is root. Niu Niu wants to prune the leaf nodes of the current binary tree, but Niu Niu cannot directly delete the leaf nodes. He can only prune the parent node of the leaf node. After pruning the parent node, the leaf node will be deleted accordingly. Niuniu wants to prune all the leaf nodes while leaving as many nodes as possible. Please return the pruned binary tree.

Remarks: I couldn’t write this question, but I reproduced it with reference to a big guy’s Python code. The first value of the 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;
	}
};

third programming question

Topic: Niumei gave Niuniu an array a of positive integers with subscripts starting from zero with a length of n. The careless Niuniu accidentally deleted some of the numbers. If ai is deleted, then ai=0. For all deleted numbers, Niu Niu chooses a positive integer to fill in. Now Niu Niu wants to know how many filling schemes make: a0≤a1≤...≤an-1 and satisfy 1≤ai≤k for all 0≤i≤n-1. The function passes in an array a whose subscript starts from 0 and a positive integer k. Please return the value modulo 10^9+7 of the number of legal filling schemes to ensure that there is no data whose number of schemes is 0.

Remarks: I know that dynamic programming should be used for this question but I can’t write it out. The following 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 novice. I write a blog for the purpose of summarizing and communicating. I implore everyone to criticize and correct me!

Guess you like

Origin blog.csdn.net/embracestar/article/details/119946009
Recommended