Given an array of integers, every element appears twice except for one. Find the single one.
Note:
Your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?
Hide TagsHash Table Bit manipulationThe title requirement is a specific condition, and only one numeric value appears once
1 classSolution {2 Public:3 intSinglenumber (intA[],intN) {4 intx;5 for(size_t i=0; i<n; ++i)6X ^=A[i];7 returnx;8 }9};
Status:AcceptedRuntime:18 mssubmitted:3 months, 3 weeks ago but I think the x here is not initialized, find C++primer version fifth, P40 page has the corresponding default initialization definition: "If a variable of a built-in type is not being displayed initialized, Its value is determined by the location of the definition. Variables defined outside of any function body are initialized to 0. Built-in type variables defined inside the function body will not be initialized. "The main function is also a function, so the global variable is initialized to 0, is the member function in the class initialized to 0?" This is not yet confirmed. So I changed the next
1 intSinglenumber (intA[],intN) {2 if(N <1)3 returnNULL;4 intx = a[0];5 for(size_t i=1; i<n; ++i)6X ^=A[i];7 returnx;8}
At this time unexpectedly does not have AC, prompt compile Error
Line 19:no matching function for call to ' Solution::singlenumber (std::vector<int>&) '
"Leetcode" 136.Excel Sheet Column Title