1. 程式人生 > >Single Number問題及解法

Single Number問題及解法

問題描述:

Given an array of integers, every element appearstwiceexcept 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;
    }
};