Very simple, but I found that my algorithm is very inefficient, in fact, there are many areas need to improve. One order, convenient to find. Returned as soon as it was discovered. The idea is simple, the efficiency is worrying.
class Solution {public: int missingnumber (vector<int>& Nums) { sort (nums.begin (), Nums.end ()); for (int01); + + i ) {if(i! = Nums[i]) { return i; } }};
Last chat to a different or operation, I found that the number of use this particularly convenient, because the most natural number we already know, is the length of nums. So the largest of this natural number, and 0 XOR or operation to get 0 or 1, and then with this result and nums stored in the number of different or, assuming that the number exists then return is 0, assuming that the number does not exist to return is this number.
class solution { public : int Missingnumber ( Vector<int >& Nums) { int len = Nums.size (); int xOR = Len; for (int i = 0 ; I! = Len;++ i) {XOR ^= I; XOR ^= Nums[i]; return XOR; }};
The horizontal groove, as if efficiency has improved. But this is all blind cat catch dead mouse, no point of technical content ah.
Leetcode268--missingnum