题解 | #农场牛的标识#

农场牛的标识

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

知识点:异或

思路:任何一个数与0异或等于其本身,任何一个数与自身异或等于0

编程语言:java

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param nums int整型一维数组
     * @return int整型
     */
    public int singleNumber (int[] nums) {
        // write code here
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            res ^= nums[i];
        }
        return res;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务