首页 > 其他 > 详细

LeetCode周赛190

时间:2020-05-30 18:18:48      阅读:32      评论:0      收藏:0      [点我收藏+]

LeetCode周赛190题解

技术分享图片

class Solution {
public:
    void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c){
        std::string::size_type pos1, pos2;
         pos2 = s.find(c);
        pos1 = 0;
        while(std::string::npos != pos2)
         {
             v.push_back(s.substr(pos1, pos2-pos1));
 
             pos1 = pos2 + c.size();
             pos2 = s.find(c, pos1);
        }
          if(pos1 != s.length())
            v.push_back(s.substr(pos1));
    }
    
    int isPrefixOfWord(string sentence, string searchWord) {
        vector<string> t;
        int pos = 0;
        int len = searchWord.length();
        SplitString(sentence, t, " ");
        for (int i = 0; i < t.size(); i++) {
            if (t[i].substr(0, len) == searchWord) {
                
                return i + 1;
            } 
            
        }
        
        return -1;
    }
};


技术分享图片

class Solution {
public:
    bool check(char a, string s) {
        for (int j = 0; j < 5; j++) 
           if (a == s[j]) return true;
        return false;
    }
    
    int maxVowels(string s, int k) {
        string a = "aeiou";
        int max = 0;
        vector<int> n(s.length(), 0);
        
        for (int i = 0; i  < s.length(); i++) {
            
                if (check(s[i], a)) {
                    n[i] = 1;
                    
                } 
            
        }
        
        for (int i = 0; i < k; i++) {
            if (n[i] == 1) max++;
        }
        
        int cnt = max;
        for (int i = k ; i < s.length(); i++) {
            if (check(s[i], a)) { //是元音
                if (n[i - k] == 1) continue; //右移以后左边元音被移除
                else {
                    cnt++; //左边不是元音
                }
            } else { //不是元音
                if (n[i - k] == 1) cnt--; //左边元音被移出
            }
            max = max < cnt ? cnt : max;
        }
        return max;

    }
};

LeetCode周赛190

原文:https://www.cnblogs.com/DengSchoo/p/12993819.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!