Single Number问题及解法

来源:互联网 发布:xshell for mac 编辑:程序博客网 时间:2024/06/09 18:09

问题描述:

Given an array of integers, every element appears twice except for one. Find that single one.

问题分析:

本题可用位运算------异或(相同的数异或为0,任何数与0异或均为0)


过程详见代码:

class Solution {public:    int singleNumber(vector<int>& nums) {        int res = 0;        for(int i = 0;i < nums.size(); i++)        {        res ^= nums[i];}return res;    }};




0 0