【leetcode】Single Number

版权声明:噗噗个噗~~~ https://blog.csdn.net/Puyar_/article/details/85261807

[编程题] single-number

时间限制:1秒

空间限制:32768K

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?

  Solution:XOR,an integer and its own exclusive or after the value is zero, zero and other integers exclusive or obtained is the 

integer itself.

class Solution {
public:
    int singleNumber(int A[], int n) {
		int res=0;
	    for(int i=0;i<n;i++)
           res^=A[i];
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/Puyar_/article/details/85261807