(一)求一个数组中只出现一次的数

 1 public class NumAppearOnce {
 2     /*
 3     思想,
 4     1、单个数的1位肯定是奇数个,0位就是0
 5     2、因此1位相异或就是1,0位异或还是0
 6     3、即所有数异或就是单个数本身
 7     例子:
 8     1 0 1 1 0 1 0 0 0
 9     0 0 1 1 1 0 1 0 1
10     1 1 1 1 1 1 1 1 1
11     1 0 1 1 0 1 0 0 0
12     1 1 1 1 1 1 1 1 1
13      */
14     public int oneNumAppearOnce(int [] nums){
15         int res = 0;
16         for(int n : nums)
17             res ^= n;
18         return res;
19     }
20 }

猜你喜欢

转载自www.cnblogs.com/ylxn/p/10362687.html