[Leetcode string processing] Compare Version Numbers, leetcodecompare

Source: Internet
Author: User

[Leetcode string processing] Compare Version Numbers, leetcodecompare
[Leetcode string processing] Compare Version Numbers

@ Author: wepon @ blog: http://blog.csdn.net/u012162613
1. Question

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

You may assume that the version strings are non-empty and contain only digits and.Character.
The.Character does not represent a decimal point and is used to separate number sequences.
For instance,2.5Is not "two and a half" or "half way to version three", it is the second-level revision of the second first-level revision.

Here is an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37

2. Analysis question meaning: the meaning of the question is clear, that is, compare the "version number". The given version1 and version2 are string-type. When version1> version2, 1 is returned, otherwise,-1 is returned.
This question is a detail processing question, except for the tedious string processing.
Solution: first, separate the version1 and version2 strings into multiple substrings according to '.', and convert each substring into an integer and store it in the container. Finally, compare the size of the number of corresponding positions in the two containers. Of course, we need to consider the case that they have different lengths.
Note: (1) form of version: 10.23.1, 01.23, 1.2.3.4... and so on (2) the string is converted into an integer, and my program directly uses the library function stoi ()
3. Code
Class Solution {public: int compareVersion (string version1, string version2) {vector <int> result1 = getInt (version1); vector <int> result2 = getInt (version2 ); int len1 = result1.size (); int len2 = result2.size (); if (len2 <len1) return-1 * compareVersion (version2, version1); int I = 0; while (I <len1 & result1 [I] = result2 [I]) I ++; if (I = len1) {// str1 and str2 before len1 are equal, then look at str2 after the len2-len1 bit is 0 can judge their size int j = len2-1; While (j> = len1) {if (result2 [j --]! = 0) return-1;} return 0;} else {// The len1 digits before str1 and str2 are not equal, directly determine the I-bit if (result1 [I] <result2 [I]) return-1; else return 1 ;}} private: // press the version string '. 'split into multiple, convert to an integer and put it into the container vector <int> getInt (string version) {vector <int> result; int len = version. size (); int pre = 0; for (int I = 0; I <len; I ++) {if (version [I] = '. ') {string str (version. begin () + pre, version. begin () + I); // note that this initialization form is left closed and right open, that is, str does not include version [version. begin () + I] result. push_back (stoi (str); pre = I + 1 ;}} string str (version. begin () + pre, version. end (); result. push_back (stoi (str); return result ;}};


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.