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 };

Single Number_第1张图片

你可能感兴趣的:(number)