首页 > 其他 > 详细

leetcode1282

时间:2019-12-08 11:53:01      阅读:90      评论:0      收藏:0      [点我收藏+]
 1 class Solution:
 2     def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:
 3         dic = {}
 4         n = len(groupSizes)
 5         for i in range(n):
 6             if groupSizes[i] not in dic:
 7                 dic[groupSizes[i]] = [i]
 8             else:
 9                 dic[groupSizes[i]].append(i)
10         res = []
11         for k,v in dic.items():
12             #k:每组几个,v集合
13             temp = []
14             for j in range(len(v)):
15                 temp.append(v[j])
16                 if len(temp) == k:
17                     res.append(temp[:])
18                     temp.clear()
19         return res

哈希思想,在dic中记录每一种size对应的元素的集合。

然后按照size的大小进行分组,每个子集中都包含size个元素。

leetcode1282

原文:https://www.cnblogs.com/asenyang/p/12005115.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!