@XQF
2018-03-07T22:57:02.000000Z
字数 547
阅读 1204
数据结构与算法
public class Solution {
public int findNotSingle(int[] nums) {
if (nums == null || nums.length == 0 || nums.length == 1) {
return -1;
}
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
int temp = nums[i] ^ nums[i + 1];
if (temp == 0) {
System.out.println(nums[i]);
return nums[i];
}
}
return -1;
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums = {1, 2, 3, 4, 5, 6, 6};
solution.findNotSingle(nums);
}
}
把数值作为一个数组的索引,将该数组的值取反,只出现一次为负数,要是出现两次,就会进行两次取反,两次取反后的结果为正数