一、原理
二、实践
2.1、幸运N串
- 滑动窗口 *
@return */ public static int sliding_window(String luckCharN) { if (luckCharN == null || luckCharN.length() == 0) {
return 0;
} //滑动窗口左右边界 int right = 0; int left = 0; //最长 N字符 int max = 0; //不是 字符N的个数 int notN = 0; char[] chars = luckCharN.toCharArray(); int len = chars.length;
while (right < len) {
if (chars[right++] != 'N') {
//右边界如果不是 N,记录个数
notN++;
}
//个数超过两个
while (notN > 2) {
//滑动窗口左边界移动
if (chars[left++] != 'N') {
notN--;
}
}
max = Math.max(max, right - left);
} return max; } ```