Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit series activities – click on the task to see the details of the activities

describe

Given two version numbers, version1 and version2, compare them.

Version numbers consist of one or more revisions joined by a dot ‘.’. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33 and 0.1 are valid version numbers.

To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.

Return the following:

  • If version1 < version2, return -1.
  • If version1 > version2, return 1.
  • Otherwise, return 0.

Example 1:

Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation: Ignoring leading zeroes, both "01" and "001" represent the same integer "1".
Copy the code

Example 2:

Input: version1 = "0.1", version2 = "1.1"
Output: -1
Explanation: version1's revision 0 is "0", while version2's revision 0 is "1". 0 < 1, so version1 < version2.
Copy the code

Note:

1 <= version1.length, version2.length <= 500
version1 and version2 only contain digits and '.'.
version1 and version2 are valid version numbers.
All the given revisions in version1 and version2 can be stored in a 32-bit integer.
Copy the code

parsing

Given two version numbers, version1 and version2, compare their sizes. The version number consists of one or more numeric strings with the dot “. The composition of the concatenation, each numeric string may contain leading zeros, these are used with “. Separated numeric strings are commonly referred to as major version numbers, minor version numbers, minor version numbers, and so on. To compare version number, according to the order from left to right, compare their two major version number represents the size of the integer Numbers, and then compare their two version number represents the size of the integer number the first time, and so on, if there are leading zeros can be ignored, if at some stage in which a version number, with zero filling continue to compare. During the comparison, 1 is returned as long as the integer of one phase of version1 is greater than the integer of one phase of version2, -1 is returned if the integer is less than, and 0 is returned if the integer is equal until the end.

The above topic said a lot of in fact is to introduce the composition of version number and comparison rules, is more cumbersome, we can understand through observing examples to understand the meaning of the question, the idea is relatively simple:

  • Replace the version1 and version2 strings with “. Split list
  • Take the longest length of both and complete the shorter list with 0
  • It then iterates over the integer values a and b of the same index of both lists, returning 1 directly if a > b, -1 directly if a
  • Return 0 at the end of the loop

As long as you understand the topic, it is still very simple, the main topic is too tedious, let a person daunting.

answer

class Solution(object): def compareVersion(self, version1, version2): """ :type version1: str :type version2: str :rtype: int """ L1 = version1.split('.') L2 = version2.split('.') max_L = max(len(L1), len(L2)) if len(L1) ! = max_L: L1 += [0] * (max_L-len(L1)) else: L2 += [0] * (max_L-len(L2)) for i in range(max_L): a,b = int(L1[i]), int(L2[i]) if a>b: return 1 elif a<b: return -1 elif a==b: continue return 0Copy the code

The results

Given the submission in the Python online submission list. Submissions in Python online submissions for Compare Version Numbers.Copy the code

The original link

Leetcode.com/problems/co…

Your support is my biggest motivation