第 190 场周赛

5416. 检查单词是否为句中其他单词的前缀

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
bool check(string a, string b) {
if(a.size() < b.size()) return false;
for(int i=0; i<b.size(); i++) {
if(a[i]!=b[i]) return false;
}
return true;
}
int isPrefixOfWord(string sentence, string searchWord) {
stringstream ss;
ss << sentence;
vector<string> cot;
string cur;
while(ss >> cur) {
cot.push_back(cur);
}
for(int i=0; i<cot.size(); i++) {
if(check(cot[i], searchWord)) {
return i+1;
}
}
return -1;
}
};

5417. 定长子串中元音的最大数目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int maxVowels(string s, int k) {
unordered_map<char, int> mp;
int curmax = 0;
for(int i=0; i<s.size(); i++) {
mp[s[i]]++;
if(i >= k-1) {
curmax = max(curmax, mp['a']+mp['e']+mp['i']+mp['o']+mp['u']);
mp[s[i-(k-1)]]--;
}
}
return curmax;
}
};

5418. 二叉树中的伪回文路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* 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:
int ans = 0;
void dfs(vector<int>& count, TreeNode* root) {
if(root == NULL) return;
if(root->left == NULL && root->right == NULL) {
count[root->val]++;
int f1 = 0;
for(int i=1; i<=9; i++) {
if(count[i] % 2 == 1) f1++;
}
if(f1 <= 1) ans++;
count[root->val]--;
return;
}
count[root->val] ++;
dfs(count, root->left);
dfs(count, root->right);
count[root->val] --;
}
int pseudoPalindromicPaths (TreeNode* root) {
vector<int> count(10);
dfs(count, root);
return ans;
}
};

5419. 两个子序列的最大点积

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int maxDotProduct(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size(), m = nums2.size();
vector<vector<int>> f(n+1, vector<int>(m+1,-5000000));
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
f[i][j] = max({nums1[i-1]*nums2[j-1], f[i][j-1], f[i-1][j], f[i-1][j-1]+nums1[i-1]*nums2[j-1]});
}
}
return f[n][m];
}
};
Donate? comment?