题目链接:https://leetcode.com/problems/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:
[5, 3]
is
also correct.public class Solution { public int[] singleNumber(int[] nums) { int[] result=new int[2]; List<Integer> temp=new ArrayList<Integer>(); for(int i=0;i<nums.length;i++) { //如果不存在,则加入temp中 if(!temp.contains(nums[i])) { temp.add(nums[i]); } //不存在,表示存在两次,就从temp除去该数 else { temp.remove((Object)nums[i]); } } result[0]=temp.get(0); result[1]=temp.get(1); return result; } }
【LeetCode OJ 260】Single Number III
原文:http://blog.csdn.net/xujian_2014/article/details/50581165