您的位置:首页 > 其它

LeetCode学习(4)Single Number

2017-10-21 19:58 253 查看
1.问题描述

Given an array of integers, every element appears twice except for one. Find that single one.Your algorithm should have a linear runtime complexity.

给定一组整数,除了一个元素外,每个元素都出现两次。找到那一个。你的算法应该具有线性运行时复杂性。

2.代码时刻

//c++ VS2013
#include<iostream>
using namespace std;
int singleNumber(int A[],int n) {
int result = 0;
for (int i = 0; i<n; i++)
{
result ^= A[i];//重点用到了异或判断的方法
}
return result;
}
int main()
{
int A[100] = { 1, 2, 3, 4,6, 15, 1, 2, 3,  4, 6 };
cout << singleNumber(A,13);
cout << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode