前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode(Weekly Contest 190)题解

LeetCode(Weekly Contest 190)题解

作者头像
西凉风雷
发布2022-11-23 19:45:08
2060
发布2022-11-23 19:45:08
举报
文章被收录于专栏:容器与 Kubernetes 游记

0. 前言

1. 题解

1.1 5416. 检查单词是否为句中其他单词的前缀(1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence)

代码语言:javascript
复制
class Solution {
public:
    int isPrefixOfWord(string sentence, string searchWord) {
        int len = sentence.length();
        if (sentence[len-1] != ' ') {
            sentence += " ";
            len++;
        }
        int last = 0;
        vector<string> words;
        for (int i = 1 ; i< len ; i++) {
            if (sentence[i] == ' ') {
                words.push_back(sentence.substr(last, i-last));
                last = i + 1;
            }
        }
        for (int i = 0 ; i < (int)words.size() ; i++) {
            int lv = words[i].length(), ls = searchWord.length();
            if (lv < ls)    continue;
            if (words[i].substr(0, ls) == searchWord)  return i+1;
        }
        return -1;
    }
};

1.2 5417. 定长子串中元音的最大数目(1456. Maximum Number of Vowels in a Substring of Given Length)

代码语言:javascript
复制
class Solution {
public:
    int maxVowels(string s, int k) {
        int len = (int)s.length(), cnt = 0, ans = 0;
        for (int i = 0 ; i < k ; i++) {
            if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') {
                cnt++;
            }
        }
        ans = cnt;
        for (int i = k ; i < len ; i++) {
            if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u') {
                cnt++;
            }
            if (s[i-k] == 'a' || s[i-k] == 'e' || s[i-k] == 'i' || s[i-k] == 'o' || s[i-k] == 'u') {
                cnt--;
            }
            ans = max(ans, cnt);
        }
        return ans;
    }
};

1.3 5418. 二叉树中的伪回文路径(1457. Pseudo-Palindromic Paths in a Binary Tree)

代码语言:javascript
复制
/**
 * 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:
    void dfs(TreeNode* root, vector<int>& cnt, int& ans) {
        if (root->left == NULL && root->right == NULL) {
            int sum = 0, odd = 0;
            for (int i = 1 ; i <= 9 ; i++) {
                if (cnt[i] % 2) odd++;
                sum += cnt[i];
            }
            if (sum % 2 == odd)    ans++;
            return;
        }
        if (root->left) {
            cnt[root->left->val]++;
            dfs(root->left, cnt, ans);
            cnt[root->left->val]--;
        }
        if (root->right) {
            cnt[root->right->val]++;
            dfs(root->right, cnt, ans);
            cnt[root->right->val]--;
        }
    }
    int pseudoPalindromicPaths (TreeNode* root) {
        if (root == NULL)   return 0;
        vector<int> cnt = vector<int>(10, 0);
        cnt[root->val]++;
        int ans = 0;
        dfs(root, cnt, ans);
        cnt[root->val]--;
        return ans;
    }
};

1.4 5419. 两个子序列的最大点积(1458. Max Dot Product of Two Subsequences)

代码语言:javascript
复制
class Solution {
public:
    int maxDotProduct(vector<int>& nums1, vector<int>& nums2) {
        int n1 = (int)nums1.size(), n2 = (int)nums2.size(), len = min(n1, n2);
        vector<vector<int>> dp = vector<vector<int>>(n1+1, vector<int>(n2+1, -50000000));
        int ans = INT_MIN;
        for (int i = 0 ; i <= n1 ; i++) {
            dp[i][0] = 0;
        }
        for (int i = 0 ; i <= n2 ; i++) {
            dp[0][i] = 0;
        }
        for (int j = 1 ; j <= n1 ; j++) {
            for (int k = 1 ; k <= n2 ; k++) {
                if (j > 1 && k > 1) {
                    dp[j][k] = dp[j-1][k-1]; 
                }
                if (j > 1) {
                    dp[j][k] = max(dp[j][k], dp[j-1][k]);
                }
                if (k > 1) {
                    dp[j][k] = max(dp[j][k], dp[j][k-1]);
                }
                dp[j][k] = max(dp[j][k], max(dp[j-1][k-1] + nums1[j-1] * nums2[k-1], nums1[j-1] * nums2[k-1]));
                // cout << j << "<>" << k << "==" << dp[j][k] << " | " << ans << endl;
                ans = max(ans, dp[j][k]);
            } 
        }
        return ans;
    }
};

3. 参考文献 

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-05-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0. 前言
  • 1. 题解
    • 1.1 5416. 检查单词是否为句中其他单词的前缀(1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence)
      • 1.2 5417. 定长子串中元音的最大数目(1456. Maximum Number of Vowels in a Substring of Given Length)
        • 1.3 5418. 二叉树中的伪回文路径(1457. Pseudo-Palindromic Paths in a Binary Tree)
          • 1.4 5419. 两个子序列的最大点积(1458. Max Dot Product of Two Subsequences)
          • 3. 参考文献 
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档