面试遇见了找大串的问题
/**
* 给定一个字符串其中包含任意组连续字符,我们把超过3个连续相同字符的组合称作大串,请找出它们的起止位置。如:
* ”叽叽汪汪汪喵喵喵喵喳喳” 可分组为”叽叽”,”汪汪汪”,”喵喵喵喵”,”喳喳”,其中”汪汪汪”和”喵喵喵喵”为大串组。
*/
public static void main(String[] args) {
String param = "这是啊啊啊啊啊哈哈哈嗯嗯嗯";
//0 1 2 3 45 6 7 8 9101112
System.out.println(run(param));
}
private static List<List<Integer>> run(String param) {
List<List<Integer>> result = new ArrayList<>();
char before = param.charAt(0);
int number = 0;
int start = 0;
int end = 0;
for (int i = 0; i < param.length(); i++) {
char after = param.charAt(i);
if (after == before) {
number++;
} else {
start++;
}
if (after != before) {
if (number >= 2) {
result.add(Arrays.asList(start - 1, end - 1));
start = end;
number = 0;
} else {
start = end;
}
}
if (i == param.length() - 1) {
result.add(Arrays.asList(start, end));
break;
}
end++;
before = after;
}
return result;
}
有在面试的小伙伴可以加我v wjy550823795或者加群