[LeetCode]Single Number

来源:互联网 发布:淘宝企业店铺转让过程 编辑:程序博客网 时间:2024/05/19 20:47

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?


本题是一道编程之美上的微软面试题,根据异或性质,可n-1次循环完成。

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


0 0
原创粉丝点击