Leetcode#165compare Version Numbers

Source: Internet
Author: User

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

Main problem input string there are several cases

    1. A string that does not contain a.

    2. A string containing multiple points

    3. A 0 suffix exists after a string that contains a point, for example, a comparison of 1.0.0 and 1


For a string. operation, when using split to cut the string, pay attention to the need to add "\ \"

public class Solution {

public int Compareversion (string version1, String version2) {

String w1[]=version1.split ("\ \");

String w2[]=version2.split ("\ \");

int l1=w1.length;

int l2=w2.length;

int l=l1;

if (L2<L1)

L=L2;

int i=0;

for (i=0;i<l;i++)

{

int A=integer.parseint (w1[i]);

int B=integer.parseint (w2[i]);

if (a<b)

return-1;

if (a>b)

return 1;

}

if (L1<L2)

{

for (i=l;i<l2;i++)

if (Integer.parseint (W2[i]) >0)

return-1;

return 0;

}

else if (L1>L2)

{

for (i=l;i<l1;i++)

if (Integer.parseint (W1[i]) >0)

return 1;

return 0;

}

Else

return 0;


}

}


Leetcode#165compare 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.