Given an array of numbers nums
, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given nums = [1, 2, 1, 3, 2, 5]
, return [3, 5]
.
代码如下:
public class Solution {
public int[] singleNumber(int[] nums) {
if(nums.length==2)
return nums; Arrays.sort(nums);
int k=0;
int[]a=new int[2];
for(int i=0;i<nums.length-1;)
{
if(nums[i]==nums[i+1])
i=i+2;
else{
a[k]=nums[i];
i=i+1;
if(k==1)
return a;
else k++;
}
}
a[k]=nums[nums.length-1];
return a;
}
}