leetcode 136. Single Number ----- java

时间:2022-09-27 16:22:12

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

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

求出数组中只出现了一次的数(其他的数都出现了2次)

异或求解。

public class Solution {
public int singleNumber(int[] nums) {
int len = nums.length;
int result = nums[0];
for( int i = 1;i<len;i++){ result = result^nums[i]; }
return result;
}
}