230. Compare Version Numbers

MediumStringString

Given two version strings version1 and version2, compare them. Return -1 if version1 < version2, 1 if version1 > version2, and 0 if equal. Each version is dot-separated integers; ignore leading zeros and treat missing trailing revisions as 0. Input: '"v1", "v2"'.

Input: '"v1", "v2"'.

Output: Integer — -1, 0, or 1.

Examples

Example 1
Input: "1.01", "1.001"
Output: 0
Explanation: Both equal 1.1 numerically.
Example 2
Input: "1.0", "1.0.0"
Output: 0
Explanation: Trailing zeros ignored.
Example 3
Input: "0.1", "1.1"
Output: -1
Explanation: 0 < 1.

Constraints

Asked by

AppleMicrosoftAmazonGoogle
Solve this problem in the editor →