leetcode--165 Compare Version Numbers (comparison of digital versions)

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's an example of version numbers ordering:0.1 < 1.1 < 1.2 < 13.37

Hide tags:string

Problem Solving Ideas:
(1) Use SPLT ("\ \") ) is separated by a decimal point and then compared

(2) During the comparison, we assume that the length of the array is the same. Short-length arrays, when compared to the last element, automatically add 0 actions


The code is as follows:

public static int Version (string version1, String version2) {//uses a delimiter and then compares string[] str1=version1.split ("\ \"); String[] Str2=version2.split ("\ \"); /Get the large int Length=math.max (str1.length, str2.length) that requires a scale length of two version, and for (int i = 0; i < length; i++) {/* * The following two lines of code mean the length of the two version Set to the same level as * version1=1.1 version2=1.1.2 * Execute int num1=i<str1.length? Integer.parseint (Str1[i]): 0 After * version1=1.1.0, so convenient to compare */int num1=i<str1.length? Integer.parseint (Str1[i]): 0;int num2=i<str2.length? Integer.parseint (Str2[i]): 0;if (num1>num2) {return 1;} else if (num1<num2) {return-1;}} return 0;}

leetcode--165 Compare Version Numbers (comparison of digital versions)

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.