给出一列数来构造数组,数组每一行由不同的数字组成,每行长度可以不一样,输出其中一种可行解
用python的Counter统计每个数字出现次数,再一次构造每一行
1 #2610
2 #Runtime: 60 ms (Beats 13.89%)
3 #Memory: 13.3 MB (Beats 80.56%)
4
5 class Solution(object):
6 def findMatrix(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: List[List[int]]
10 """
11 ans = []
12 cnt = Counter(nums)
13 while cnt:
14 tp = []
15 used = []
16 for i, v in cnt.items():
17 tp.append(i)
18 cnt[i] -= 1
19 if cnt[i] == 0:
20 used.append(i)
21 for i in used:
22 cnt.pop(i)
23 ans.append(tp)
24 return ans