136. Single Number

Posted zjuhaohaoxuexi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了136. 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,两个相同的数异或的结果为0
2,一个数和0异或的结果为这个数

代码:

 1 class Solution {
 2 public:
 3     int singleNumber(int A[], int n) {
 4 
 5         int result = 0;
 6         for(int i = 0;i < n;i++)
 7         {
 8             result ^= A[i];
 9         }
10         return result;
11         
12     }
13 };

 

以上是关于136. Single Number的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode之136. Single Number

136.Single Number

LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III

136. Single Number

136. Single Number

136. Single Number