public class Solution1282 {
public static void main(String[] args) {
int[] i= {3,3,3,3,3,1,3,4,4,4,4};
List<List<Integer>> list=groupThePeople(i);
for (List<Integer> list2 : list) {
System.out.println(list2);
}
}
public static List<List<Integer>> groupThePeople(int[] groupSizes) {
List<List<Integer>> res=new ArrayList<>();
Map<Integer,List<Integer>> map=new HashMap<>();
for (int i = 0; i < groupSizes.length; i++) {
if(!map.containsKey(groupSizes[i]))
map.put(groupSizes[i], new ArrayList<>());
List<Integer> temp=map.get(groupSizes[i]);
temp.add(i);
if(temp.size()==groupSizes[i]) {
res.add(new ArrayList<>(temp));
temp.clear();
}
}
return res;
}
转载请注明原文地址:https://blackberry.8miu.com/read-38549.html