第 190 场周赛 Posted on 2020-05-24 | In leetcode , Weekly-Contest | | Words count in article: 467 | Reading time ≈ 2 5416. 检查单词是否为句中其他单词的前缀12345678910111213141516171819202122232425class 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. 定长子串中元音的最大数目123456789101112131415class 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. 二叉树中的伪回文路径12345678910111213141516171819202122232425262728293031323334353637/** * 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. 两个子序列的最大点积12345678910111213class 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? Donate WeChat Pay Alipay