LeetCode-136 SingleNumber

来源:互联网 发布:modo软件多少钱 编辑:程序博客网 时间:2024/05/16 09:40

Given an array of integers, every element appears twice except for one. Find that single one.

给一个数组,找到单独的那一个数


异或:如果a、b两个值不相同,则异或结果为1。如果a、b两个值相同,异或结果为0。

输入 运算符 输入 结果 1 ⊕ 0 1 1 ⊕ 1 0 0 ⊕ 0 0 0 ⊕ 1 1
public class Solution {    public int singleNumber(int[] nums) {        int c=0;        for(int i=0;i<nums.length;i++){           c = c^nums[i];        }        return c;    }}
0 0