Leetcode:Single Number

题目链接

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?

分析:所有元素异或,最终结果就是出现一次的数                                                                                               本文地址

 1 class Solution {
 2 public:
 3     int singleNumber(int A[], int n) {
 4         // Note: The Solution object is instantiated only once and is reused by each test case.
 5         int res = 0;
 6         for(int i = 0; i < n; i++)
 7             res = res^A[i];
 8         return res;
 9     }
10 };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3422329.html

原文地址:https://www.cnblogs.com/TenosDoIt/p/3422329.html