165. Compare Version Numbers Leetcode Python

Source: Internet
Author: User

Compare numbers version1 and Version2.
If Version1 > Version2 return 1, if Version1 < Version2 return-1, otherwise return 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.5 are not "both and a half" or "half to version three", it is the fifth Second-level revision of the SEC Ond first-level revision.


Here are an example of version numbers ordering:


0.1 < 1.1 < 1.2 < 13.37
Credits:

Special thanks to @ts for adding this problem and creating all test cases.

This question is mainly about unequal lengths.

My practice is to add 0 to the front when the time is not equal.

Class solution:    # @param version1, a string    # @param version2, a string    # @return An integer    def compareve Rsion (self, Version1, version2):        num1=version1.split ('. ')        Num2=version2.split ('. ')        While Len (NUM1) or Len (num2):            If Len (NUM1) ==0:                num1=[0]            elif len (num2) ==0:                num2=[0]            else:                I1=int (num1[0])                i2=int (num2[0])                if i1<i2:                    return-1                elif i1>i2:                    return 1                Else:                    num1=num1[1:]                    num2=num2[1:]        return 0            


165. Compare Version Numbers leetcode Python

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.