首页 > 代码库 > LeetCode 136. Single Number

LeetCode 136. Single Number

先排序,然后从头查找。若i = i + 1则为一对数,i = i  + 2,继续判断。若i != i + 1,则输出i。

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int t;
        if(nums.size() == 1)
        {
            t = nums[0];
        }
        else
        {
            sort(nums.begin(), nums.end());
            for(int i = 0; i < nums.size(); i++)
            {
                if(nums[i] == nums[i+1])
                {
                    i++;
                }
                else
                {
                    t = nums[i];
                }
            }
        }
        return t;
    }
};

 

LeetCode 136. Single Number