leetcode1282

时间:2019-12-08 12:33:30   收藏:0   阅读:87
 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个元素。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!