伴你如风、护你如影|

xzh-yyds

园龄:3年9个月粉丝:0关注:2

leetcode1282-用户分组

用户分组

  • 哈希分类

给每一个组别容量分配一个List,存入哈希表中。遍历数组,将当前下标加入对应数量的List中。
如果List数量满了,那么将其从map中删除并存入返回值。

class Solution {
    public List<List<Integer>> groupThePeople(int[] groupSizes) {
        List<List<Integer>> list = 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<Integer>());
            List<Integer> l = map.get(groupSizes[i]);
            l.add(i);
            if(l.size() == groupSizes[i]){
                list.add(l);
                map.remove(groupSizes[i]);
            }
        }
        return list;
    }
}

本文作者:xzh-yyds

本文链接:https://www.cnblogs.com/xzh-yyds/p/16590150.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   xzh-yyds  阅读(24)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开