zoukankan      html  css  js  c++  java
  • 1371. 每个元音包含偶数次的最长子字符串

    class Solution {
        public int findTheLongestSubstring(String s) {
            int n = s.length();
            int[] pos = new int[32]; // 11111最大为31,记录所有状态可能的情况开32就可以了
            // pos[status] 记录的是前多少个字符可以使状态变为status!
            Arrays.fill(pos,-1);
            pos[0] = 0;  // 前0个字符可以使状态变为0
            int res = 0, status = 0; // 5位整数表示出现aeiou的状态,0代表这一位为偶数个,1为奇数个
            for(int i = 0; i < n; i++) {
                char c = s.charAt(i);
                if(c == 'a') {
                    status ^= 1 << 0;
                } else if (c == 'e') {
                    status ^= 1 << 1;
                } else if (c == 'i') {
                    status ^= 1 << 2;
                } else if (c == 'o') {
                    status ^= 1 << 3;
                } else if (c == 'u') {
                    status ^= 1 << 4;
                }                         // 奇数-偶数=奇数  偶数-奇数= 奇数
                if(pos[status] != -1) {  // 所以 只有奇数-奇数 偶数-偶数 才会出现偶数,所以只有之前出现过这个状态,之间的个数才是偶数次
                    res = Math.max(res,i+1-pos[status]);
                } else {
                    pos[status] = i + 1;
                }
            }
            return res;
        }
    }
  • 相关阅读:
    final有什么用?
    数组的定义
    作业
    List 、Set数据结构
    报表工具实现单据套打
    动态格报表的制作
    图形钻取
    报表工具轻松搞定卡片式报表
    列表钻取
    报表中如何实现不规则布局
  • 原文地址:https://www.cnblogs.com/yonezu/p/13360222.html
Copyright © 2011-2022 走看看