0
点赞
收藏
分享

微信扫一扫

165. Compare Version Numbers**

165. Compare Version Numbers**

​​https://leetcode.com/problems/compare-version-numbers/​​

题目描述

Compare two version numbers ​​version1​​​ and ​​version2​​​.
If ​​​version1 > version2​​​ return ​​1​​​; if ​​version1 < version2​​​ return ​​-1​​​;otherwise return ​​0​​.

You may assume that the version strings are non-empty and contain only digits and the ​​.​​ character.

The ​​.​​ character does not represent a decimal point and is used to separate number sequences.

For instance, ​​2.5​​ 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.

You may assume the default revision number for each level of a version number to be ​​0​​​. For example, version number ​​3.4​​​ has a revision number of ​​3​​​ and ​​4​​​ for its first and second level revision number. Its third and fourth level revision number are both ​​0​​.

Example 1:

Input: version1 = "0.1", version2 = "1.1"
Output: -1

Example 2:

Input: version1 = "1.0.1", version2 = "1"
Output: 1

Example 3:

Input: version1 = "7.5.2.4", version2 = "7.5.3"
Output: -1

Example 4:

Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation: Ignoring leading zeroes, both “01” and “001" represent the same number “1”

Example 5:

Input: version1 = "1.0", version2 = "1.0.0"
Output: 0
Explanation: The first version number does not have a third level revision number, which means its third level revision number is default to "0"

Note:

  • Version strings are composed of numeric strings separated by dots​​.​​ and this numeric stringsmayhave leading zeroes.
  • Version strings do not start or end with dots, and they will not be two consecutive dots.

C++ 实现 1

对 version number 按照 ​​.​​​ 进行 split (要是用 Python 的话, 直接 ​​version.split('.')​​​ 就特别方便了), 这个功能可以使用 ​​getline​​​ 实现, 在 ​​71. Simplify Path**​​​ 我就采用过. 使用 ​​std::stoi()​​​ 可以将字符串转换为整数, 该函数可以处理函数 leading zero 的字符串, 比如说可以传入 ​​"001"​​ 这样的字符串.

class Solution {
public:
int compareVersion(string version1, string version2) {
stringstream ss1(version1), ss2(version2);
string u;
vector<string> num1, num2;
while (getline(ss1, u, '.')) num1.push_back(u);
while (getline(ss2, u, '.')) num2.push_back(u);
int i = 0, j = 0;
while (i < num1.size() || j < num2.size()) {
auto n1 = i >= num1.size() ? 0 : std::stoi(num1[i]);
auto n2 = j >= num2.size() ? 0 : std::stoi(num2[j]);
if (n1 > n2) return 1;
else if (n1 < n2) return -1;
++ i, ++ j;
}
return 0;
}
};

C++ 实现 2

不采用 stringstream:

class Solution {
public:
// 注意可能有 "01.1.1" 这样的情况, 所以里面还有两个 while 循环用于处理
// "01" 连续几个数的情况.
int compareVersion(string version1, string version2) {
int ver1 = 0, ver2 = 0; // 用于保存每个 level 的 version
int i = 0, j = 0;
while (i < version1.size() || j < version2.size()) {
// 两个 while 循环用于求每个 level 的 version
while (isdigit(version1[i]) && i < version1.size())
ver1 = ver1 * 10 + version1[i++] - '0';

while (isdigit(version2[j]) && j < version2.size())
ver2 = ver2 * 10 + version2[j++] - '0';

// 比较每个 level 的 version
if (ver1 < ver2)
return -1;
else if (ver1 > ver2)
return 1;

ver1 = 0, ver2 = 0; // 用于下一个 level 的比较
// 前面两个 while 循环中 i 和 j 已经指向了 '.',
// 这里将 '.' 跳过.
i ++;
j ++;
}
// 只有每个 level 的 version 都相等, 才返回 0;
return 0;
}
};

举报

相关推荐

0 条评论