题解 | #只出现一次的数字(二)#

只出现一次的数字(二)

https://www.nowcoder.com/practice/85cb47fc0c6c483fab7e5cefab54d9e5

using System;
using System.Collections.Generic;


class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param nums int整型一维数组
     * @return int整型
     */
    public int singleNumber (List<int> nums) {
        // write code here
        if (nums == null || nums.Count == 0)
            return 0;
        if (nums.Count == 1)
            return nums[0];
        int nR = 0;
        for (int n = 31; n >= 0; n--) {
            int nSum = 0;
            for (int i = 0; i < nums.Count; i++)
                nSum += (nums[i] >> n & 1);
            nR = (nSum % 3) + 2 * nR;
        }
        return nR;
    }
}

全部评论

相关推荐

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