题解 | #牛的品种排序II#

牛的品种排序II

https://www.nowcoder.com/practice/43e49fbb98b4497ba46e185918188b1c

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;
    }
};

虚数五行区解题中心 文章被收录于专栏

非淡泊无以明志,非宁静无以致远

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务