Leetcode 165. Compare Version Numbers String

Source: Internet
Author: User

165. Compare Version Numbers

Compare numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return-1, otherwise ret Urn 0.

Assume that the version strings is non-empty and contain only digits and the.Character.
The.Character does not represent a decimal point and was used to separate number sequences.
For instance,2.5is not "both and a half" or "half-to-version three", it is the fifth Second-level revision of the second first-level re Vision.

Here are an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37


Ideas:

1. Divide the two versions of the string into a vector.

2. Compare the size of the elements in the 2 vectors.

Note: 1 = = 1.0.0


Class solution {public:    vector<int> stringsplit (String s,  const char * split)     {         vector<int> result;        const int slen  = s.length ();        char *cs = new  char[slen + 1];        strcpy (Cs, s.data ());         char *p;        char  * end;        p = strtok (Cs, split);         while  (P)         {             printf ("%s\n",  p);         &nbsP;   string tmp (P);             int v = static_cast<int> (Strtol (Tmp.c_str (), &end,10));             result.push_back (v);             p = strtok (Null, split);         }        return result;    }         int compareversion (string version1, string  version2)  {        vector<int> vecint1 =  stringsplit (Version1, ".");         vector<int> vecint2 = stringsplit ( Version2, ".");                 int I;        for (I = 0; i < min (VecInt1.size ( ), Vecint2.size ());  i++)         {             if (Vecint1[i] < vecint2[i])                   return -1;             else if (Vecint1[i] > vecint2[i])                 return 1;         }                 if ( vecint1.size ()  < vecint2.size ())          {            int  j = i; &Nbsp;          for (; J <vecint2.size (); j + +)              {                 if (vecint2[j] > 0)                      return -1 ;            }             return 0;        }         else if (Vecint1.size ()  > vecint2.size ())          {             int j = i;            for (; j  < vecint1.sizE (); j + +)             {                 if (vecint1[j] > 0)                       return 1;            }             return 0;         }        else             return 0;    }};


This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1836522

Leetcode 165. Compare Version Numbers String

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.