码迷,mamicode.com
首页 > 其他好文 > 详细

Single Number

时间:2014-09-18 23:33:14      阅读:292      评论:0      收藏:0      [点我收藏+]

标签: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 }

 

屌爆

Single Number

标签:style   blog   color   ar   for   div   sp   log   on   

原文地址:http://www.cnblogs.com/sweetculiji/p/3980226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!