​LeetCode刷题实战165:比较版本号

程序IT圈

共 3661字,需浏览 8分钟

 · 2021-01-26

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 比较版本号  ,我们先来看题面:
https://leetcode-cn.com/problems/compare-version-numbers/

Version numbers consist of one or more revisions joined by a dot '.'. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33 and 0.1 are valid version numbers.


To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.


Return the following:


If version1 < version2, return -1.

If version1 > version2, return 1.

Otherwise, return 0.

题意


给你两个版本号 version1 和 version2 ,请你比较它们。

版本号由一个或多个修订号组成,各修订号由一个 '.' 连接。每个修订号由 多位数字 组成,可能包含 前导零 。每个版本号至少包含一个字符。修订号从左到右编号,下标从 0 开始,最左边的修订号下标为 0 ,下一个修订号下标为 1 ,以此类推。例如,2.5.33 和 0.1 都是有效的版本号。

比较版本号时,请按从左到右的顺序依次比较它们的修订号。比较修订号时,只需比较 忽略任何前导零后的整数值 。也就是说,修订号 1 和修订号 001 相等 。如果版本号没有指定某个下标处的修订号,则该修订号视为 0 。例如,版本 1.0 小于版本 1.1 ,因为它们下标为 0 的修订号相同,而下标为 1 的修订号分别为 0 和 1 ,0 < 1 。

返回规则如下:

  • 如果 version1 > version2 返回 1,

  • 如果 version1 < version2 返回 -1,

  • 除此之外返回 0。





样例

示例 1

输入:version1 = "1.01", version2 = "1.001"
输出:0
解释:忽略前导零,"01" 和 "001" 都表示相同的整数 "1"

示例 2

输入:version1 = "1.0", version2 = "1.0.0"
输出:0
解释:version1 没有指定下标为 2 的修订号,即视为 "0"

示例 3

输入:version1 = "0.1", version2 = "1.1"
输出:-1
解释:version1 中下标为 0 的修订号是 "0",version2 中下标为 0 的修订号是 "1" 。0 < 1,所以 version1 < version2

示例 4

输入:version1 = "1.0.1", version2 = "1"
输出:1

示例 5

输入:version1 = "7.5.2.4", version2 = "7.5.3"
输出:-1



解题

https://blog.csdn.net/qq_41855420/article/details/87918821

思路:

如果直接在字符串中进行修改比较,是会比较麻烦的,所以将版本号的各级修订号都转化为int型,这样就比较好处理了。

class Solution {
public:
  int compareVersion(string version1, string version2) {
        int versionOneSize = version1.size();
    int versionTwoSize = version2.size();
        int maxSize = max(versionOneSize, versionTwoSize);//获取长度的最大值
    vector<int> numsVersionOne(maxSize, 0);//用于存放version1各级修订号的字符子串转化为int
    vector<int> numsVersionTwo(maxSize, 0);//用于存放version2各级修订号的字符子串转化为int
    int tempValue = 0, cnt = 0;
    //将version1各级修订号转化为数字
    for (int index = 0; index < versionOneSize; ++index) {
      tempValue = 0;
      while (index < versionOneSize && version1[index] != '.') {
        tempValue = tempValue * 10 + version1[index] - '0';
        ++index;
      }
      numsVersionOne[cnt++] = tempValue;
    }
    cnt = 0;
    //将version2各级修订号转化为数字
    for (int index = 0; index < versionTwoSize; ++index) {
      tempValue = 0;
      while (index < versionTwoSize && version2[index] != '.') {
        tempValue = tempValue * 10 + version2[index] - '0';
        ++index;
      }
      numsVersionTwo[cnt++] = tempValue;
    }
    //将两个版本号的四个数字逐位进行比较
    for (int i = 0; i < maxSize; ++i) {
      if (numsVersionOne[i] > numsVersionTwo[i]) {
        return 1;
      }
      else if (numsVersionOne[i] < numsVersionTwo[i]) {
        return -1;
      }
      //剩下的就是numsVersionOne[i] == numsVersionTwo[i],需要继续比较下一位
    }
    return 0;//最后四位都比较完了,还没有分出胜负
  }
};


好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。

上期推文:

LeetCode1-160题汇总,希望对你有点帮助!
LeetCode刷题实战161:相隔为1的编辑距离
LeetCode刷题实战162:寻找峰值
LeetCode刷题实战163:缺失的区间
LeetCode刷题实战164:最大间距


浏览 20
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报