题解 | #牛群中的特异牛#

牛群中的特异牛

https://www.nowcoder.com/practice/a83ed72354bc4504b5c5f471eb972f42

#include <algorithm>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums int整型vector 
     * @return int整型
     */
    int findUniqueCow(vector<int>& nums) {
        // write code here
        sort(nums.begin(), nums.end());
        if (nums[0] != nums[1]) return nums[0]; // 1 2 2 2
        for (int i = 2; i < nums.size(); i += 3) {
            if (nums[i] != nums[i - 2]) return nums[i - 2]; // 1 1 1 2 2 2 3
        }
        return nums.back(); // 说明 nums 的最后一个元素是单独的
    }
};

不会算法就排序了再做

全部评论

相关推荐

虚闻松声:继续投吧。 简历没啥问题。很优秀。 拙见:自我评价没什么意义;试试转向Agent开发、大模型应用;别死磕传统Java开发。 免费修改简历,就业咨询,欢迎私信交流。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务