题解 | #出现一次的牛#

出现一次的牛

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

using System;
using System.Collections.Generic;

class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param nums int整型一维数组 
     * @return int整型
     */
    public int findSingleCow (List<int> nums) {
        // write code here
        Dictionary<int,int> res = new Dictionary<int,int>();
        foreach(int num in nums)
        {
            if(res.ContainsKey(num))
            {
                if(res[num]==1)
                {
                    res[num]++;
                }
                else
                {
                    res.Remove(num);
                }
            }
            else
            {
                res.Add(num, 1);
            }
        }
        ICollection<int> keys = res.Keys;
        foreach (int key in keys)
        {
            return key;
        }
        return -1;
    }
}

全部评论

相关推荐

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