Single Number

Question:

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?

Solution:

 1 class Solution {
 2 public:
 3     int singleNumber(vector<int>& nums) {
 4     sort(nums.begin(),nums.end());
 5     for(int i=0;i<nums.size();i+=2)
 6     {
 7         if(nums[i]!=nums[i+1])
 8         {
 9             return nums[i];
10         }
11     }    
12     }
13 };

posted on 2015-07-08 22:43  Riden  阅读(217)  评论(0编辑  收藏  举报

导航