class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param cows int整型vector
* @return int整型vector
*/
vector<int> sortCows(vector<int>& cows) {
// write code here
//方法一: 冒泡排序
// 方法二:利用哈希表重构数组
unordered_map<int,int> u_m;
for(auto cow:cows)
++u_m[cow];
int temp=0;
int index = 0;
while(temp<3)
{
while(u_m[temp]>0)
{
cows[index++] = temp;
--u_m[temp];
}
++temp;
}
return cows;
}
};

京公网安备 11010502036488号