Leetcode 165 Compare Version Numbers

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

Solution:
The main idea was very simple and the code consists of three phases:
1.When Version1 and Version2 is not finished, compare the value of corresponding string before dot.
2.If Version1 is finished, check whether remaining Version2 contains string not equal to 0
3.If Version2 is finished, check whether remaining version1 contains string not equal to 0

example1:version1== "11.22.33", version2== "11.22.22". 11 = = 11; 22 = = 22; > 22; Return 1.

example2:version1== "11.22.33", version2== "11.22.33". 11 = = 11; 22 = = 22; 33 = = 33; return 0.

example3:version1== "11.22.33", version2== "11.22.33.00.00". 11 = = 11; 22 = = 22; 33 = = 33; Remaining version2 equals to 0; return 0.

example4:version1== "11.22.33.00.01", version2== "11.22.33". 11 = = 11; 22 = = 22; 33 = = 33; Remaining Version1 contains 01; Return 1.

Class Solution { Public:int compareversion(stringVersion1,stringVersion2) {inti =0;intj =0;intN1 = Version1.size ();intN2 = Version2.size ();intNUM1 =0;intnum2 =0; while(I < n1 | | J < n2) { while(I<n1 && version1[i]!='. ') {NUM1 = num1*Ten+ (version1[i]-' 0 ');            i++; } while(J<n2 && version2[j]!='. ') {num2 = num2*Ten+ (version2[j]-' 0 ');            j + +; }if(NUM1&GT;NUM2)return 1;Else if(NUM1&LT;NUM2)return-1; NUM1 =0; num2 =0;            i++;        j + +; }return 0; }};

Python solution:

 class solution:    # @param A, a string    # @param B, a string    # @return A Boolean     def compareversion(self, version1, version2):V1 = Version1.split ('. ') v2 = Version2.split ('. ') forIinchRange (max (len (v1), Len (v2))): Gap = (int (v1[i])ifI < Len (v1)Else 0)-(int (v2[i])ifI < Len (v2)Else 0)ifGap! =0:return 1 ifGap >0 Else-1        return 0

Leetcode 165 Compare Version Numbers

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.