java.lang.Object
g0101_0200.s0165_compare_version_numbers.Solution

public class Solution extends Object
165 - Compare Version Numbers.<p>Medium</p> <p>Given two version numbers, <code>version1</code> and <code>version2</code>, compare them.</p> <p>Version numbers consist of <strong>one or more revisions</strong> joined by a dot <code>'.'</code>. Each revision consists of <strong>digits</strong> and may contain leading <strong>zeros</strong>. Every revision contains <strong>at least one character</strong>. Revisions are <strong>0-indexed from left to right</strong> , with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example <code>2.5.33</code> and <code>0.1</code> are valid version numbers.</p> <p>To compare version numbers, compare their revisions in <strong>left-to-right order</strong>. Revisions are compared using their <strong>integer value ignoring any leading zeros</strong>. This means that revisions <code>1</code> and <code>001</code> are considered <strong>equal</strong>. If a version number does not specify a revision at an index, then **treat the revision as <code>0</code> **. For example, version <code>1.0</code> is less than version <code>1.1</code> because their revision 0s are the same, but their revision 1s are <code>0</code> and <code>1</code> respectively, and <code>0 < 1</code>.</p> <p><em>Return the following:</em></p> <ul> <li>If <code>version1 < version2</code>, return <code>-1</code>.</li> <li>If <code>version1 > version2</code>, return <code>1</code>.</li> <li>Otherwise, return <code>0</code>.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> version1 = &ldquo;1.01&rdquo;, version2 = &ldquo;1.001&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> Ignoring leading zeroes, both &ldquo;01&rdquo; and &ldquo;001&rdquo; represent the same integer &ldquo;1&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> version1 = &ldquo;1.0&rdquo;, version2 = &ldquo;1.0.0&rdquo;</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> version1 does not specify revision 2, which means it is treated as &ldquo;0&rdquo;.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> version1 = &ldquo;0.1&rdquo;, version2 = &ldquo;1.1&rdquo;</p> <p><strong>Output:</strong> -1</p> <p><strong>Explanation:</strong> version1&rsquo;s revision 0 is &ldquo;0&rdquo;, while version2&rsquo;s revision 0 is &ldquo;1&rdquo;. 0 < 1, so version1 < version2.</p> <p><strong>Example 4:</strong></p> <p><strong>Input:</strong> version1 = &ldquo;1.0.1&rdquo;, version2 = &ldquo;1&rdquo;</p> <p><strong>Output:</strong> 1</p> <p><strong>Example 5:</strong></p> <p><strong>Input:</strong> version1 = &ldquo;7.5.2.4&rdquo;, version2 = &ldquo;7.5.3&rdquo;</p> <p><strong>Output:</strong> -1</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= version1.length, version2.length <= 500</code></li> <li><code>version1</code> and <code>version2</code> only contain digits and <code>'.'</code>.</li> <li><code>version1</code> and <code>version2</code> <strong>are valid version numbers</strong>.</li> <li>All the given revisions in <code>version1</code> and <code>version2</code> can be stored in a <strong>32-bit integer</strong>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • compareVersion

      public int compareVersion(String version1, String version2)