腾讯音乐笔试
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param preOrder int整型vector
* @param inOrder int整型vector
* @return TreeNode类vector
*/
vector<TreeNode*> getBinaryTrees(vector<int>& preOrder, vector<int>& inOrder) {
if(0 == preOrder.size()) {
return {nullptr};
}
vector<TreeNode *> rslt;
for(int pos(0); pos < inOrder.size(); ++pos) {
if(inOrder[pos] == preOrder[0]) {
vector<int> preLeft(preOrder.begin() + 1, preOrder.begin() + pos + 1);
vector<int> inLeft(inOrder.begin(), inOrder.begin() + pos);
vector<int> preRight(preOrder.begin() + pos + 1, preOrder.end());
vector<int> inRight(inOrder.begin() + pos + 1, inOrder.end());
vector<TreeNode *> left(getBinaryTrees(preLeft, inLeft));
vector<TreeNode *> right(getBinaryTrees(preRight, inRight));
for(const auto &lc : left) {
for(const auto &rc : right) {
TreeNode *root = new TreeNode(preOrder[0]);
root->left = lc;
root->right = rc;
rslt.push_back(root);
}
}
}
}
return rslt;
}
}; class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 返回满足题意的最小操作数
* @param str string字符串 给定字符串
* @return int整型
*/
int minOperations(string str) {
int times[26] = {0}, idleCnt(0), rslt(0);
for(const auto c : str) {
times[c - 'a']++;
}
for(int i(0); i < 26; ++i) {
if(0 == times[i] % 2) {
++idleCnt;
}
rslt += times[i] / 2;
}
return rslt <= idleCnt ? rslt : rslt * 2 - idleCnt;
}
}; 感觉不难,但只A了2道 离开屏幕前1次(不超过5秒),手机开监考的时候一直收到微信嗡嗡响,切出去一次(不超过10秒),没关系吧

