知识点
遍历,模拟
思路
用一个map建立从i到i出现次数的映射,遍历cows数组,每出现一次就在对应出现次数处++。遍历完以后,对于整个map进行遍历,向ans数组中插入i对应出现次数个i。返回ans即为答案。时间复杂度为O(nLOGn)
代码
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param cows int整型vector
* @param k int整型
* @return int整型vector
*/
vector<int> sortCowsIII(vector<int>& cows, int k) {
// write code here
map<int,int>mp;
for(auto v:cows)
{
mp[v]++;
}
vector<int>ans;
for(auto v:mp)
{
for(int i=1;i<=v.second;i++)
{
ans.push_back(v.first);
}
}
return ans;
}
};