标签:style blog class code tar ext
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
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?
题意:在一组数组中除一个元素外其它元素都出现两次,找出这个元素
思路:位运算。异或。因为异或操作可以交换元素的顺序,所以元素异或的顺序没影响,推广:这个方法也适合于出现其它元素都出现偶数次,而要找的元素出现奇数次的情况
复杂度:时间O(n),空间O(1)
相关题目:Single Number IIclass Solution { public: int singleNumber(int A[], int n) { int temp = 0; for(int i = 0; i < n; i++){ temp = temp ^ A[i]; } return temp; } };
Leetcode 位运算 Single Number,布布扣,bubuko.com
标签:style blog class code tar ext
原文地址:http://blog.csdn.net/zhengsenlie/article/details/25509679