LeetCode:Single Number

来源:互联网 发布:网络零售商 编辑:程序博客网 时间:2024/06/17 00:54

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?

<span style="font-size:18px;">int singleNumber(int A[], int n){    int result = 0;    for(int i = 0;i<n;i++)        result ^= A[i];    return result;}</span>
偶数个相同的整数异或后为0,所以遍历数组,与所有的数组元素相继异或,最后异或的结果就是单独 的整数

已AC 36ms

0 0
原创粉丝点击