1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-03-14 16:22:24 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/比较版本号 [compare-version-numbers].html

61 lines
2.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你两个 <strong>版本号字符串</strong>&nbsp;<code>version1</code><code>version2</code> ,请你比较它们。版本号由被点&nbsp;<code>'.'</code> 分开的修订号组成。<strong>修订号的值</strong> 是它 <strong>转换为整数</strong> 并忽略前导零。</p>
<p>比较版本号时,请按 <strong>从左到右的顺序</strong> 依次比较它们的修订号。如果其中一个版本字符串的修订号较少,则将缺失的修订号视为 <code>0</code></p>
<p>返回规则如下:</p>
<ul>
<li>如果&nbsp;<code><em>version1&nbsp;</em>&lt;&nbsp;<em>version2</em></code> 返回 <code>-1</code></li>
<li>如果&nbsp;<code><em>version1&nbsp;</em>&gt;&nbsp;<em>version2</em></code>&nbsp;返回&nbsp;<code>1</code></li>
<li>除此之外返回 <code>0</code></li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">version1 = "1.2", version2 = "1.10"</span></p>
<p><strong>输出:</strong><span class="example-io">-1</span></p>
<p><strong>解释:</strong></p>
<p>version1 的第二个修订号为&nbsp;"2"version2 的第二个修订号为 "10"2 &lt; 10所以 version1 &lt; version2。</p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">version1 = "1.01", version2 = "1.001"</span></p>
<p><strong>输出:</strong><span class="example-io">0</span></p>
<p><strong>解释:</strong></p>
<p>忽略前导零,"01" 和 "001" 都代表相同的整数 "1"。</p>
</div>
<p><strong class="example">示例 3</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">version1 = "1.0", version2 = "1.0.0.0"</span></p>
<p><strong>输出:</strong><span class="example-io">0</span></p>
<p><strong>解释:</strong></p>
<p>version1 有更少的修订号,每个缺失的修订号按 "0" 处理。</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= version1.length, version2.length &lt;= 500</code></li>
<li><code>version1</code><code>version2</code> 仅包含数字和 <code>'.'</code></li>
<li><code>version1</code><code>version2</code> 都是 <strong>有效版本号</strong></li>
<li><code>version1</code><code>version2</code> 的所有修订号都可以存储在 <strong>32 位整数</strong></li>
</ul>