您的位置:首页 > 其它

LeetCode 260

2016-04-24 23:15 148 查看
Single Number III

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]
.

Note:

The order of the result is not important. So in the above example,
[5, 3]
is also correct.

Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

public class Solution {
public int[] singleNumber(int[] nums) {

Set<Integer> set = new HashSet<Integer>();
for(int i:nums){
if(set.add(i) == false){
set.remove(i);
}
}
int a[] = new int [set.size()];
int b = 0;
for(int c:set){
a[b] = c;
b++;
}

return a;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: