0X01 题目
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?
0X02 题意
求只出现一次的数字。该数字只有一个。
0X03 题解
1.先排序(Dante:Java)
先排序,然后从头遍历一下数组,两两比较即可。
public class Solution {
public int singleNumber(int[] nums) {
if(nums.length == 1) {
return nums[0];
}
Arrays.sort(nums);
for(int i = 0; i < nums.length-1; i += 2) {
if(nums[i] != nums[i+1])
return nums[i];
}
return nums[nums.length-1];
}
}
2016-08-05 12:50:00 hzct