Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
Example 1:
1 | Input: nums = [1,1,1,2,2,3], k = 2 |
Example 2:
1 | Input: nums = [1], k = 1 |
找list中個數k的數值,可以用python Counter.most_common,裡面是heapq.nlargest,所以時間複雜度是nlogn,找出符合各數的數字放進ret回傳。
1 | T:O(nlogn), S:O(n) |
如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)