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

Single Number

时间:2014-07-22 23:07:14      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   strong   

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?

 

mamicode.com,码迷
 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 }
mamicode.com,码迷

 

Single Number,码迷,mamicode.com

Single Number

标签:style   blog   http   java   color   strong   

原文地址:http://www.cnblogs.com/Altaszzz/p/3700517.html

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