Single Number

题目:

Given 2*n + 1 numbers, every numbers occurs twice except one, find it.

分析:

这是一道背诵题,bit manipulation的方法没见过一般做不出来。不过问题是,为什么tag写的是greedy?

解法:

class Solution {
public:
    /**
     * @param A: Array of integers.
     * return: The single number.
     */
    int singleNumber(vector<int> &A) {
        // write your code here
        int res = 0;
        for (int i = 0; i < A.size(); i++) {
            res = res ^ A[i];
        }
        return res;
    }
};

results matching ""

    No results matching ""