Loading

leetcode 311场周赛总结

1、最小偶倍数(2413)

题目:给你一个正整数 n ,返回 2n 的最小公倍数(正整数)。

签到题,奇数的话就*2,偶数直接返回。

class Solution {
public:
    int smallestEvenMultiple(int n) {
        return n&1 ? 2*n : n;
    }
};

2、最长字母序连续子字符串的长度(2414)

题目:字母序连续字符串 是由字母表中连续字母组成的字符串。换句话说,字符串 abcdefghijklmnopqrstuvwxyz 的任意子字符串都是 字母序连续字符串 。

  • 例如,abc 是一个字母序连续字符串,而 acbza 不是。

给你一个仅由小写英文字母组成的字符串 s ,返回其 最长 的 字母序连续子字符串 的长度。

滑动窗口类型的题:

  • 前后两个指针,满足条件的话,右指针不断后移,窗口不断增大。
  • 否则去调整左指针。
class Solution {
public:

    // 滑动窗口
    int maxSize=1;
    int longestContinuousSubstring(string s) {
        int left=0,right=1;
        int size=s.size();
        while(right < size){
            if(s[right] == s[right-1]+1) maxSize=max(maxSize,right-left);
            else left = right;
            right++;
        }
        return maxSize;
    }
    
    // 模拟遍历,有个res值代替了right-left的操作
    int longestContinuousSubstring(string s) {
        int ans=1, res=1;
        for(int i = 1 ;i < s.size(); ++i){
            if(s[i] == s[i-1]+1) res++;
            else res = 1;
            ans = max(res,ans);  
        }
        return ans;
    }
};

滑动窗口题

209. 长度最小的子数组 - 力扣(LeetCode)

​ 给一个整数target和子数组,求长度最小的连续子数组。

​ 右指针在前面加,加完后左指针在前面循环减同时min()更新答案。

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

​ 求定长字符串中大小为k的子串内最多的元音字母的个数为多少?前闭后开的窗口往后面扫。

2415、反转完美二叉树的奇数层

奇数层的节点的val全部要反过来。

方法:

  • 还是DFS遍历,只不过左右子树分别遍历
  • 左子树 左->中->右
  • 右子树 右->中->左
  • 当到第奇数层的时候,将左右两个数的节点val交换一下

image

class Solution {
public:
    void DFS(TreeNode* lfChild,TreeNode* rgChild,int n){
        if(lfChild == nullptr) return;
        if(n & 1) swap(lfChild->val,rgChild->val);
        DFS(lfChild->left,rgChild->right,n+1);
        DFS(lfChild->right,rgChild->left,n+1);
    }
    TreeNode* reverseOddLevels(TreeNode* root) {
        if(root->left == nullptr) return root;
        DFS(root->left,root->right,1);
        return root;
    }
};

相似题:

226 翻转二叉树 遍历+swap

101对称二叉树 递归函数中的参数也是两个变量

2416. 字符串的前缀分数和

image

输入:words = ["abc","ab","bc","b"]
输出:[5,4,3,2]
解释:对应每个字符串的答案如下:
- "abc" 有 3 个前缀:"a"、"ab" 和 "abc" 。
- 2 个字符串的前缀为 "a" ,2 个字符串的前缀为 "ab" ,1 个字符串的前缀为 "abc" 。
总计 answer[0] = 2 + 2 + 1 = 5 。
- "ab" 有 2 个前缀:"a" 和 "ab" 。
- 2 个字符串的前缀为 "a" ,2 个字符串的前缀为 "ab" 。
总计 answer[1] = 2 + 2 = 4 。
- "bc" 有 2 个前缀:"b" 和 "bc" 。
- 2 个字符串的前缀为 "b" ,1 个字符串的前缀为 "bc" 。 
总计 answer[2] = 2 + 1 = 3 。
- "b" 有 1 个前缀:"b"。
- 2 个字符串的前缀为 "b" 。
总计 answer[3] = 2 。

hard题,主要是要了解一个叫 前缀树 的数据结构 + 其每个节点存储 前缀记录值 的方法(没做出来,不了解前缀树结构,可能了解也做不出来😂)。

image

class Tire{
private:
    vector<Tire*> child;
    int val;

public:
    Tire():child(26),val(0){}
    void insertWord(string word){
        Tire* node = this;
        for(char &ch : word){
            int c = ch - 'a';
            if(node->child[c] == nullptr){
                node->child[c] = new Tire();
            }
            node=node->child[c];
            node->val++;
        }
    }
    int searchWord(string word){
        Tire* node = this;
        int sum=0;
        for(char &ch : word){
            int c = ch - 'a';
            sum += node->child[c]->val;
            node = node->child[c];
        }
        return sum;
    }
};
class Solution {
public:
    vector<int> sumPrefixScores(vector<string>& words) {
        vector<int> ans;
        Tire* tree = new Tire();
        for(const string &word : words){
            tree->insertWord(word);
        }
        for(const string &word : words){
            ans.push_back(tree->searchWord(word));
        }
        return ans;
    }
};
posted @ 2022-09-24 16:26  青一菜  阅读(31)  评论(0)    收藏  举报