Leetcode 每日一题 1371. 每个元音包含偶数次的最长子字符串

1371. 每个元音包含偶数次的最长子字符串

解题思路:

官方的解题思路太好了。。。官网思路 记录一下代码,方便自己日后复查。

源代码:
class Solution {
public:
    int findTheLongestSubstring(string s) {
        // 用来保存当前序列的状态
        int c_s = 0, maxLen = 0;
        vector<int> status(100, INT_MIN);
        int i = 0, len = s.length();
        status[0] = 0;
        while(i<len){
            if(s[i] == 'a'){
                c_s ^= 1;
            }else if(s[i] == 'e'){
                c_s ^= 1<<1;
            }else if(s[i] == 'i'){
                c_s ^= 1<<2;
            }else if(s[i] == 'o'){
                c_s ^= 1<<3;
            }else if(s[i] == 'u'){
                c_s ^= 1<<4;
            }
            if(status[c_s] != INT_MIN){
                maxLen = max(maxLen, i - status[c_s] + 1);
            }else{
                status[c_s] = i + 1;
            }
            i ++;
        }
        return maxLen;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章