您的位置:首页 > 编程语言 > Java开发

Single Number

2016-05-31 15:30 288 查看
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];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java