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?
Subscribe to see which companies asked this question
我的方法貌似用的是O(n*n)的时间复杂度的。。不过第一次脱离IDE,白板编程~还是有待于提高的~~
public class Solution {
public int singleNumber(int[] nums) {
int[] arr = new int[nums.length];
int r = 0;
for(int i = 0 ;i < nums.length ; i++)
arr[i] = 0;
for(int i = 0; i < nums.length ; i++){
for(int j = i+1 ; j <nums.length ; j++)
{
if(nums[i] == nums[j])
{
arr[i]=1;
arr[j]=1;
}
}
}
for(int i = 0 ; i < nums.length ;i++){
if(arr[i]==0)
r = i;
}
return nums[r];
}
}