标签:
题目:
Given an array of integers, every element appears twice except for one. Find that single one.
解法一: bit manipulate
1 public class Solution { 2 public int singleNumber(int[] nums) { 3 int ans = 0; 4 for (int i = 0; i < nums.length; i++) ans ^= nums[i]; 5 return ans; 6 7 8 } 9 }
解法二:hash table ->不是太明白!!
1 public int singleNumber(int[] A) { 2 HashSet<Integer> set = new HashSet<Integer>(); 3 for (int n : A) { 4 if (!set.add(n)) 5 set.remove(n); 6 } 7 Iterator<Integer> it = set.iterator(); 8 return it.next(); 9 }
reference:
http://blog.csdn.net/kenden23/article/details/13625297
http://www.programcreek.com/2012/12/leetcode-solution-of-single-number-in-java/
标签:
原文地址:http://www.cnblogs.com/hygeia/p/4687851.html