标签:style blog color ar for div sp log on
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?
异或:相同为0 不同为1
0和a数异或都是a
a和a异或就是0
由于每个数有两个 所以总会相见一次 所以还是0
1 public class singleNumber { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 int a[]={1,-1,3,3,4,1,5,5,-1,6}; 8 System.out.println(singleNumber(a)); 9 10 } 11 12 // 异或 13 public static int singleNumber(int[] A) { 14 int res = 0; 15 for (int i : A) { 16 res ^= i; 17 } 18 return res; 19 } 20 21 22 }
屌爆
标签:style blog color ar for div sp log on
原文地址:http://www.cnblogs.com/sweetculiji/p/3980226.html