Leetcode268--missingnum

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.