Single Number

Single Number

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?

 

 1 public class Solution {
 2     public int singleNumber(int[] A) {
 3           if(A.length<=0)
 4             return -1;
 5         if(A.length==1)
 6             return A[0];
 7       //  Arrays.sort(A);
 8         Arrays.sort(A);
 9         int j  = 0 ;
10         for (int i=0;i<A.length-1;i++){
11             if(A[i]==A[i+1])
12                 j++;
13             else {
14                 if(j<1)
15                     return A[i];
16                 j = 0;
17             }
18 
19         }
20     return A[A.length-1];
21     }
22 }

 

posted on 2014-04-30 05:11  Atlas-Zzz  阅读(109)  评论(0编辑  收藏  举报

导航