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;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章