Leetcode:compare version Numbers-Compare edition number

Source: Internet
Author: User

1. Title

Compare version Numbers (compare version number)

2. Address of the topic

https://leetcode.com/problems/compare-version-numbers/

3. Topic content

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

Chinese: Compare two versions of Version1 and Version2, if Version1 is later than Version2, return 1 if Version1 is earlier than Version2, return 1, and other cases 0

4. Methods of Solving problems

The idea of solving the problem is to first press the two version number to "." Segment, and then compare by Segment. It is important to consider the case where a version number is identical to the first half of another version number (such as "1.0.1" in "1.0" and "1.0.1" is larger, "1.0" and "1.0.0" are the same size). The Java code is as follows:

/** *  function Description:leetcode 165 - compare version numbers *  Developer:tsybius2014 *  Development time: September 17, 2015  */public class Solution {         /**     *  Compare version number      *   @param  version1  version number 1     *  @param  version2  version number 2      *  @return      */    public int  compareversion (String version1, string version2)  {                String[] versionArray1 =  Version1.split ("\ \");         string[] versionarray2 = version2.split ("\ \");                 int len1  = versionarray1.length;        int len2 = versionarray2.length;         int len = len1 <= len2 ?  len1 : len2;        //Total version number section, from front to back compare position number          int x1, x2;        for   (int i = 0; i < len; i++)  {             x1 = integer.parseint (Versionarray1[i]);             x2 = integer.parseint (VersionArray2[i]);             if  (X1&NBSP;&GT;&NBSP;X2)  {                 return 1;             } else if  (X1&NBSP;&LT;&NBSP;X2)  {                 return -1;             }         }                // In the case of a common version number, who has more than one version number and the extra part is not 0, whose version is updated         if  (len1 > &NBSP;LEN2)  {            for  (int i  = len; i < len1; i++)  {                 if  (Integer.parseint (Versionarray1[i])  > 0)  {                     return 1;                }             }         } else if  (LEN1&NBSP;&LT;&NBSP;LEN2)  {             for  (int i = len; i < len2; i++)  {                if   (Integer.parseint (Versionarray2[i])  > 0)  {                     return -1;                 }             }        }                 return 0;    }} 

END

Leetcode:compare version Numbers-Compare edition number

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.