Nav: << previous: 164.最大间距 | next: 166.分数到小数 >>


Description

tab: English
 
<p>Given two <strong>version strings</strong>, <code>version1</code> and <code>version2</code>, compare them. A version string consists of <strong>revisions</strong> separated by dots <code>&#39;.&#39;</code>. The <strong>value of the revision</strong> is its <strong>integer conversion</strong> ignoring leading zeros.</p>
 
<p>To compare version strings, compare their revision values in <strong>left-to-right order</strong>. If one of the version strings has fewer revisions, treat the missing revision values as <code>0</code>.</p>
 
<p>Return the following:</p>
 
<ul>
	<li>If <code>version1 &lt; version2</code>, return -1.</li>
	<li>If <code>version1 &gt; version2</code>, return 1.</li>
	<li>Otherwise, return 0.</li>
</ul>
 
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">version1 = &quot;1.2&quot;, version2 = &quot;1.10&quot;</span></p>
 
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
 
<p><strong>Explanation:</strong></p>
 
<p>version1&#39;s second revision is &quot;2&quot; and version2&#39;s second revision is &quot;10&quot;: 2 &lt; 10, so version1 &lt; version2.</p>
</div>
 
<p><strong class="example">Example 2:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">version1 = &quot;1.01&quot;, version2 = &quot;1.001&quot;</span></p>
 
<p><strong>Output:</strong> <span class="example-io">0</span></p>
 
<p><strong>Explanation:</strong></p>
 
<p>Ignoring leading zeroes, both &quot;01&quot; and &quot;001&quot; represent the same integer &quot;1&quot;.</p>
</div>
 
<p><strong class="example">Example 3:</strong></p>
 
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">version1 = &quot;1.0&quot;, version2 = &quot;1.0.0.0&quot;</span></p>
 
<p><strong>Output:</strong> <span class="example-io">0</span></p>
 
<p><strong>Explanation:</strong></p>
 
<p>version1 has less revisions, which means every missing revision are treated as &quot;0&quot;.</p>
</div>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li><code>1 &lt;= version1.length, version2.length &lt;= 500</code></li>
	<li><code>version1</code> and <code>version2</code>&nbsp;only contain digits and <code>&#39;.&#39;</code>.</li>
	<li><code>version1</code> and <code>version2</code>&nbsp;<strong>are valid version numbers</strong>.</li>
	<li>All the given revisions in&nbsp;<code>version1</code> and <code>version2</code>&nbsp;can be stored in&nbsp;a&nbsp;<strong>32-bit integer</strong>.</li>
</ul>
 
 
 
> [!tip]- Hint 1
> 
> You can use two pointers for each version string to traverse them together while comparing the corresponding segments.
 
> [!tip]- Hint 2
> 
> Utilize the substring method to extract each version segment delimited by '.'. Ensure you're extracting the segments correctly by adjusting the start and end indices accordingly.
 
 
---
 
[submissions](https://leetcode.com/problems/compare-version-numbers/submissions/) | [solutions](https://leetcode.com/problems/compare-version-numbers/solutions/)
 
 
tab: 中文
 
<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>
 
 
 
> [!tip]- 提示 1
> 
> You can use two pointers for each version string to traverse them together while comparing the corresponding segments.
 
> [!tip]- 提示 2
> 
> Utilize the substring method to extract each version segment delimited by '.'. Ensure you're extracting the segments correctly by adjusting the start and end indices accordingly.
 
 
---
 
[提交记录](https://leetcode.cn/problems/compare-version-numbers/submissions/) | [题解](https://leetcode.cn/problems/compare-version-numbers/solution/)
 
 

Solutions & Notes

properties:
  note.updated:
    displayName: Last Updated
  note.relative_links:
    displayName: Related Links
  note.desc:
    displayName: Description
  note.grade:
    displayName: Rating
  note.program_language:
    displayName: Language
  note.time_complexity:
    displayName: TC
  note.space_complexity:
    displayName: SC
views:
  - type: table
    name: Solutions & Notes
    filters:
      and:
        - file.hasLink(this.file)
        - file.tags.containsAny("leetcode/solution", "leetcode/note")
    order:
      - file.name
      - desc
      - program_language
      - time_complexity
      - space_complexity
      - grade
      - relative_links
      - updated
    sort:
      - property: grade
        direction: ASC
      - property: time_complexity
        direction: ASC
      - property: program_language
        direction: ASC
    columnSize:
      file.name: 104
      note.space_complexity: 65
      note.grade: 126
 

Similar Problems

properties:
  note.lcTopics:
    displayName: Topics
  note.lcAcRate:
    displayName: AC Rate
  note.favorites:
    displayName: Favorites
  note.grade:
    displayName: Rating
  note.translatedTitle:
    displayName: Title (CN)
  note.lcDifficulty:
    displayName: Difficulty
views:
  - type: table
    name: Similar Problems
    filters:
      and:
        - file.hasLink(this.file)
        - similarQuestions.contains(this.file)
    order:
      - file.name
      - translatedTitle
      - lcTopics
      - lcDifficulty
      - lcAcRate
      - grade
      - favorites
    sort:
      - property: file.name
        direction: ASC
      - property: lcTopics
        direction: DESC
    columnSize:
      note.translatedTitle: 240
      note.lcTopics: 347
      note.lcAcRate: 75
      note.grade: 122