current position:Home>Tencent music entertainment 2022 campus recruitment c++ written test programming topic
Tencent music entertainment 2022 campus recruitment c++ written test programming topic
2022-07-21 23:45:48【embracestar】
Examination papers
The examination paper includes 4 Problem ,3 Programming problem ,1 Questions and answers , Time limit 100 minute .
The first programming problem
subject : Niuniu has a character only ’1’ To ’9’ The length of the composition is n String s, Now Niuniu can intercept a section with a length of k The substring of is treated as a positive decimal integer , For example, for substring "123", Its 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 length of n String s And a positive integer k, Please return to the answer .
remarks : Mainly investigate 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;
}
};
Input "321456987" and 3 Get the results 987.
The second programming problem
subject : Niuniu has one n A binary tree of nodes , Its root node is root. Niuniu wants to prune the leaf nodes of the current binary tree , But Niuniu cannot delete leaf nodes directly . He can only prune the parent nodes of leaf nodes , After pruning the parent node , Leaf nodes will also be deleted , Niuniu wants to leave as many nodes as possible , Trim off all leaf nodes . Please return to the pruned binary tree .
remarks : This inscription cannot be written , Later, I referred to a big man's Python Code recurrence , among pair The first value indicates whether the node is a leaf node , 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
subject : Niu Mei gave Niu Niu a length of n The subscript of is a positive integer array starting from zero a, Careless Niuniu accidentally deleted some of the figures . Join in ai Been deleted , be ai=0. For all deleted numbers , Niuniu chooses a positive integer and fills it in . Now Niuniu wants to know how many filling schemes make :a0≤a1≤…≤an-1 And for all 0≤i≤n-1 Satisfy 1≤ai≤k. Function passes in a subscript from 0 Starting array a And a positive integer k, Please return the legal number of filling scheme pairs 10^9+7 The value of the die , The number of schemes guaranteed to be nonexistent is 0 The data of .
remarks : I know that this problem should be solved by dynamic programming, but I can't write it , The following questions are from the same Python bosses , I use C++ Reappeared .
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'm just a programmer , Blogging is for summarizing and communicating , Please criticize and correct !
copyright notice
author[embracestar],Please bring the original link to reprint, thank you.
https://en.bfun.fun/2022/202/202207210514176447.html
The sidebar is recommended
- The animated film "pillow knife song: a journey to the earth" was highly praised, and the soundtrack was pleasantly online
- This Sunday evening, there will be a festive event in Nanfeng City, Hongqiao... | aishenhuo warms the heart
- "Don't forget I love you" Naza and Liu Yihao perform the most beautiful confession
- From Ling Na BEI'ER to Bing Dwen Dwen, from Liu Xiang to Gu ailing
- You are so beautiful! Anne Hathaway appeared on Broadway to support uncle wolf's musical
- The grand prize was unveiled, and Jinnan court was on the list
- It's half time, come and look around!
- Cao Jingxing, a media man, died at the age of 75 and was once called "news radar"
- Veterans will always be young, dreams will always shine | Beijing to the future focuses on the Chinese army that will never give up
- Dongxi asked | how much do you know about "Baba niangya" in the TV series "little niangya"?
guess what you like
The audience is abandoning the cinema?
The online drama "Hello, Captain" is about to start a new chapter of civil aviation drama
Exclusive dialogue -- Peng Xiaoran: pay tribute to the Winter Olympic athletes, who are familiar with northeast cuisine
"Our hot life" Chen Xiaochun's first experience of legal aid Zhang Qi's visit to women's prison tears collapse
Bloom the beauty of Winter Olympics with "the name of ice and snow"
Naza basks in the photo of Lord snow eagle, holding flowers and smiling sweetly
Friends returns: why is this play so happy? Because it is the portrayal of our ideal life
Happy thousands of smiles show the new year's new atmosphere
Retro beauty! Zhang Zifeng's self portrait of his face is super cute to the camera
After 12 years as a supporting actor, he was almost expelled from happy fried dough twist. Now Wei Xiang is successful as the male number one?
Random recommended
- Ten male stars of "drama dishonor": if there are more such actors, the audience will not be poisoned by the rotten film
- From the king of TV viewing to the queen of turntable, why is Xie Na, the "first sister of satellite TV", less and less likeable
- On the thirty six strategies of Chinese traditional art of war from "sniper"
- Chen Suping: years flow, keeping the miss of Gu Yuezhen and the soul of "hard work"
- Screenwriter Wang Hailing talks about behind the scenes: not all life should be open, and "the world" can comfort ordinary people
- Rebirth entertainment is developing a new single FPS and the same universe as apex hero
- Rebirth entertainment may develop a new single FPS game based on the universe of apex heroes
- Farewell film curtain call performance, giant screen cinema of China Science and Technology Museum will start digital transformation
- Detective 7 pays attention to campus bullying, judges' panel online popularization of legal knowledge
- Li Fei directed the warm comedy "Nobel's gift" and started it. Zhang Benyu, Han Haolin, pan Binlong explored the true meaning of family affection
- Female star data exposure, Liu Yifei is the most popular, and Yang Mi's baby is not in the top three
- "I want to go to your world and love you" released the theme song MV Jiao Maiqi's warm singing of the theme song of the premiere film
- 40% red line! SARFT continues to regulate sky high film remuneration
- Essence of SIFF film school | Dennis Villeneuve: pursuing poetry and intuition in the dunes of film
- Liu Yu, a member of into1, took a funny group photo for his teammate Bo Yuan's birthday
- Workplace gongdou? Fashion circle melon collection? Yuan Yongyi, Song Jia, song Zuer's new play "dress up" is a bit of a hit
- The costume action movie "Daming flying fish suit" appeared on the international stage as a dark horse
- What is the experience of walking on the red carpet in school uniforms
- Urban workplace drama "dress up" is popular, and the old depression points to the rules of workplace survival
- Simple and thick epic masterpiece | after the view of CCTV reality TV series "human world"
- "Xieyongqiang" he Shufeng's recent situation revealed that his wife and daughter are in the family of three next to him, warm and happy
- The original music and drama of Chaoyang Court won three awards in the 6th Ping An China "three wechat" competition
- Come in and copy the answer! The right way to "sprinkle dog food" on Valentine's Day is coming! Be careful when you are single
- Bing Dwen Dwen and Shuey Rhon Rhon join CCTV 2022 Lantern Festival Party
- What are the top ten Chinese films at the box office in Hong Kong?
- 2022 Jiangsu Satellite TV Lantern Festival Gala official declaration of the 15th day of the first month to celebrate the reunion festival
- The Winter Olympics is in full swing, and "ice and snow are burning" is staged synchronously, with national style cross-border skiing under the blue sky
- Resolutely oppose "sky high price film pay"! There are rules for actors' remuneration
- "Stay zero, stay cool", Chengdu krypton center unlocks a new way of leisure and entertainment
- Car entertainment system - common sense about vehicles
- Let's make a weekly plan, learn the concept knowledge on Monday and Tuesday, practice on Wednesday and Thursday, summarize experience on Friday, rest and entertainment on Saturday and Sunday.
- AI era: how does the Internet change the way we consume entertainment?
- "Stay zero, stay cool", Chengdu krypton center unlocks a new way of leisure and entertainment
- AI translation helps social pan entertainment applications and global barrier free communication
- How to realize Android entertainment live broadcast / game special sound effect
- Four entertainment venues in Fengxian District of Shanghai were investigated and punished for operating without authorization in violation of epidemic prevention regulations
- Sensor tower: in 2021, 8.7 billion entertainment applications were downloaded worldwide
- AI translation helps social pan entertainment applications and global barrier free communication
- [entertainment] Python simple implementation of fixed length queue
- Insiders: not only entertainment and communication, wireless charging technology can provide more functions and uses for electric vehicles in the future