较大分组的位置
程序员文章站
2022-06-21 13:46:15
问题描述样例输入:代码如下:package Leetcode;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class LargeGroupPositions { public static List
- > largeGroupPositions(String s) { List
问题描述
样例输入:
代码如下:
package Leetcode;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LargeGroupPositions {
public static List<List<Integer>> largeGroupPositions(String s) {
List<List<Integer>> list=new ArrayList<>();
if (s.length()<=2) return list;
int count=1;//用于记录字符个数
for (int i = 1; i <s.length() ; i++) {
if (String.valueOf(s.charAt(i)).equals(String.valueOf(s.charAt(i-1))))
{
count++;
}
else
{
if (count>=3)
{
List<Integer> list1=new ArrayList<>();
list1.add(i-count);
list1.add(i-1);
list.add(list1);
//list.add(Arrays.asList(i-count,i-1));
}
count=1;
}
}
if (count>=3)
{
List<Integer> list1=new ArrayList<>();
list1.add(s.length()-count);
list1.add(s.length()-1);
// list.add(Arrays.asList(s.length()-count,s.length()-1)); 简写方法
list.add(list1);
}
return list;
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String s=scanner.nextLine();
List out=largeGroupPositions(s);
System.out.println(out.toString());
}
}
结果如下:
本文地址:https://blog.csdn.net/lianggege88/article/details/112223122