|
11 | 11 |
|
12 | 12 | ## [165. Compare Version Numbers (Medium)](https://leetcode.com/problems/compare-version-numbers "比较版本号")
|
13 | 13 |
|
14 | | -<p>Compare two version numbers <em>version1</em> and <em>version2</em>.<br /> |
15 | | -If <code><em>version1</em> > <em>version2</em></code> return <code>1;</code> if <code><em>version1</em> < <em>version2</em></code> return <code>-1;</code>otherwise return <code>0</code>.</p> |
| 14 | +<p>Given two version numbers, <code>version1</code> and <code>version2</code>, compare them.</p> |
16 | 15 |
|
17 | | -<p>You may assume that the version strings are non-empty and contain only digits and the <code>.</code> character.</p> |
18 | | -<p>The <code>.</code> character does not represent a decimal point and is used to separate number sequences.</p> |
19 | | -<p>For instance, <code>2.5</code> is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.</p> |
20 | | -<p>You may assume the default revision number for each level of a version number to be <code>0</code>. For example, version number <code>3.4</code> has a revision number of <code>3</code> and <code>4</code> for its first and second level revision number. Its third and fourth level revision number are both <code>0</code>.</p> |
| 16 | +<ul> |
| 17 | +</ul> |
21 | 18 |
|
22 | | -<p> </p> |
| 19 | +<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> |
| 20 | + |
| 21 | +<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 <strong>treat the revision as <code>0</code></strong>. 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> |
| 22 | + |
| 23 | +<p><em>Return the following:</em></p> |
| 24 | + |
| 25 | +<ul> |
| 26 | + <li>If <code>version1 < version2</code>, return <code>-1</code>.</li> |
| 27 | + <li>If <code>version1 > version2</code>, return <code>1</code>.</li> |
| 28 | + <li>Otherwise, return <code>0</code>.</li> |
| 29 | +</ul> |
23 | 30 |
|
| 31 | +<p> </p> |
24 | 32 | <p><strong>Example 1:</strong></p>
|
| 33 | + |
25 | 34 | <pre>
|
26 | | -<strong>Input:</strong> <code><em>version1</em></code> = "0.1", <code><em>version2</em></code> = "1.1" |
27 | | -<strong>Output:</strong> -1</pre> |
| 35 | +<strong>Input:</strong> version1 = "1.01", version2 = "1.001" |
| 36 | +<strong>Output:</strong> 0 |
| 37 | +<strong>Explanation:</strong> Ignoring leading zeroes, both "01" and "001" represent the same integer "1". |
| 38 | +</pre> |
28 | 39 |
|
29 | 40 | <p><strong>Example 2:</strong></p>
|
| 41 | + |
30 | 42 | <pre>
|
31 | | -<strong>Input: </strong><code><em>version1</em></code> = "1.0.1", <code><em>version2</em></code> = "1" |
32 | | -<strong>Output:</strong> 1</pre> |
| 43 | +<strong>Input:</strong> version1 = "1.0", version2 = "1.0.0" |
| 44 | +<strong>Output:</strong> 0 |
| 45 | +<strong>Explanation:</strong> version1 does not specify revision 2, which means it is treated as "0". |
| 46 | +</pre> |
33 | 47 |
|
34 | 48 | <p><strong>Example 3:</strong></p>
|
| 49 | + |
35 | 50 | <pre>
|
36 | | -<strong>Input:</strong> <code><em>version1</em></code> = "7.5.2.4", <code><em>version2</em></code> = "7.5.3" |
37 | | -<strong>Output:</strong> -1</pre> |
| 51 | +<strong>Input:</strong> version1 = "0.1", version2 = "1.1" |
| 52 | +<strong>Output:</strong> -1 |
| 53 | +<strong>Explanation:</strong> version1's revision 0 is "0", while version2's revision 0 is "1". 0 < 1, so version1 < version2. |
| 54 | +</pre> |
38 | 55 |
|
39 | 56 | <p><strong>Example 4:</strong></p>
|
| 57 | + |
40 | 58 | <pre>
|
41 | | -<strong>Input:</strong> <code><em>version1</em></code> = "1.01", <code><em>version2</em></code> = "1.001" |
42 | | -<strong>Output:</strong> 0 |
43 | | -<strong>Explanation:</strong> Ignoring leading zeroes, both "01" and "001" represent the same number "1"</pre> |
| 59 | +<strong>Input:</strong> version1 = "1.0.1", version2 = "1" |
| 60 | +<strong>Output:</strong> 1 |
| 61 | +</pre> |
44 | 62 |
|
45 | 63 | <p><strong>Example 5:</strong></p>
|
| 64 | + |
46 | 65 | <pre>
|
47 | | -<strong>Input:</strong> <code><em>version1</em></code> = "1.0", <code><em>version2</em></code> = "1.0.0" |
48 | | -<strong>Output:</strong> 0 |
49 | | -<strong>Explanation:</strong> The first version number does not have a third level revision number, which means its third level revision number is default to "0"</pre> |
| 66 | +<strong>Input:</strong> version1 = "7.5.2.4", version2 = "7.5.3" |
| 67 | +<strong>Output:</strong> -1 |
| 68 | +</pre> |
50 | 69 |
|
51 | 70 | <p> </p>
|
| 71 | +<p><strong>Constraints:</strong></p> |
52 | 72 |
|
53 | | -<p><strong>Note:</strong></p> |
54 | | -<ol> |
55 | | -<li>Version strings are composed of numeric strings separated by dots <code>.</code> and this numeric strings <strong>may</strong> have leading zeroes. </li> |
56 | | -<li>Version strings do not start or end with dots, and they will not be two consecutive dots.</li> |
57 | | -</ol> |
| 73 | +<ul> |
| 74 | + <li><code>1 <= version1.length, version2.length <= 500</code></li> |
| 75 | + <li><code>version1</code> and <code>version2</code> only contain digits and <code>'.'</code>.</li> |
| 76 | + <li><code>version1</code> and <code>version2</code> <strong>are valid version numbers</strong>.</li> |
| 77 | + <li>All the given revisions in <code>version1</code> and <code>version2</code> can be stored in a <strong>32-bit integer</strong>.</li> |
| 78 | +</ul> |
58 | 79 |
|
59 | 80 | ### Related Topics
|
60 | 81 | [[String](../../tag/string/README.md)]
|
0 commit comments