LeetCode-1282. 用户分组

/** 1282. 用户分组

* @author 作者 Your-Name:

* @version 创建时间:2020年2月24日 上午10:44:23

* 有 n 位用户参加活动,他们的 ID 从 0 到 n - 1,每位用户都 恰好 属于某一用户组。给你一个长度为 n 的数组 groupSizes,其中包含每位用户所处的用户组的大小,请你返回用户分组情况(存在的用户组以及每个组中用户的 ID)。

你可以任何顺序返回解决方案,ID 的顺序也不受限制。此外,题目给出的数据保证至少存在一种解决方案。

 

示例 1:

输入:groupSizes = [3,3,3,3,3,1,3]
输出:[[5],[0,1,2],[3,4,6]]
解释:
其他可能的解决方案有 [[2,1,6],[5],[0,4,3]] 和 [[5],[0,6,2],[4,3,1]]。

示例 2:

输入:groupSizes = [2,1,3,3,3,2]
输出:[[1],[0,5],[2,3,4]]

 

提示:

    groupSizes.length == n
    1 <= n <= 500
    1 <= groupSizes[i] <= n

*/

public class yonghufenzhu {
	public static void main(String[] args)
	{
		int[] groupSizes = {3,3,3,3,3,1,3};
		groupThePeople(groupSizes);
	}
	public static List<List<Integer>> groupThePeople(int[] groupSizes) {
		
		List<List<Integer>> l = new ArrayList<>();

		
		for(int i=0;i<groupSizes.length;i++)
		{
			List<Integer> list = new ArrayList<Integer>();
			int temp = groupSizes[i];
			int num=temp;
			for(int j=0;j<groupSizes.length;j++)
			{		
				if(num==0)
					break;
				if(groupSizes[j]==temp&&num>0)
				{
					list.add(j);
					groupSizes[j]=0;
					num--;
				}
			}
			if(!list.isEmpty())
				l.add(list);
		}
		System.out.println(l);
		return l;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值