boolcheck(char c){ if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') returntrue; returnfalse; }
intfunc(string str){ int index[2]; int maxlen = 0; int flag = 0; for (int i = 0; i < str.length(); ++i) { if (flag && check(str[i])) { index[1] = i; int newlen = index[1] - index[0]; if (newlen > maxlen) maxlen = newlen; index[0] = index[1]; } if (!flag && check(str[i])) {//第1个元音字母 index[0] = i; flag = 1; } } return maxlen; }
题解2
对首个元音字母的处理 进行逻辑优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
boolcheck(char c){ if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') returntrue; returnfalse; }
intfunc(string str){ int last = -1; int maxlen = 0; for (int i = 0; i < str.length(); ++i) { if (check(str[i])) { if (last == -1) last = i; else { maxlen = max(maxlen, i - last); last = i; } } } return maxlen; }
题解3
优化部分细节 集成check函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
intfunc(string str){ int ans = 0; int last = -1; for (int i = 0; str[i]; ++i) { if (str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') { if (last == -1) last = i; else { ans = max(ans, i - last); last = i; } } } return ans; }