C++实现LeetCode165.版本比较)

 更新时间:2021年07月31日 15:39:48   作者:返回主页Grandyang  
这篇文章主要介绍了C++实现LeetCode165.版本比较),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

[LeetCode] 165.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.

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

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

这道题调试了好久,一直不想上网搜别人的解法,因为感觉自己可以做出来,改来改去最后终于通过了,再上网一搜,发现果然和别人的方法不同,小有成就感。我的思路是:由于两个版本号所含的小数点个数不同,有可能是1和1.1.1比较,还有可能开头有无效0,比如01和1就是相同版本,还有可能末尾无效0,比如1.0和1也是同一版本。对于没有小数点的数字,可以默认为最后一位是小数点,而版本号比较的核心思想是相同位置的数字比较,比如题目给的例子,1.2和13.37比较,我们都知道应该显示1和13比较,13比1大,所以后面的不用再比了,再比如1.1和1.2比较,前面都是1,则比较小数点后面的数字。那么算法就是每次对应取出相同位置的小数点之前所有的字符,把他们转为数字比较,若不同则可直接得到答案,若相同,再对应往下取。如果一个数字已经没有小数点了,则默认取出为0,和另一个比较,这样也解决了末尾无效0的情况。代码如下:

解法一:

class Solution {
public:
    int compareVersion(string version1, string version2) {
        int n1 = version1.size(), n2 = version2.size();
        int i = 0, j = 0, d1 = 0, d2 = 0;
        string v1, v2;
        while (i < n1 || j < n2) {
            while (i < n1 && version1[i] != '.') {
                v1.push_back(version1[i++]);
            }
            d1 = atoi(v1.c_str());
            while (j < n2 && version2[j] != '.') {
                v2.push_back(version2[j++]);
            }
            d2 = atoi(v2.c_str());
            if (d1 > d2) return 1;
            else if (d1 < d2) return -1;
            v1.clear(); v2.clear();
            ++i; ++j;
        }
        return 0;
    }
};

当然我们也可以不使用将字符串转为整型的atoi函数,我们可以一位一位的累加,参加如下代码:

解法二:

class Solution {
public:
    int compareVersion(string version1, string version2) {
        int n1 = version1.size(), n2 = version2.size();
        int i = 0, j = 0, d1 = 0, d2 = 0;
        while (i < n1 || j < n2) {
            while (i < n1 && version1[i] != '.') {
                d1 = d1 * 10 + version1[i++] - '0';
            }
            while (j < n2 && version2[j] != '.') {
                d2 = d2 * 10 + version2[j++] - '0';
            }
            if (d1 > d2) return 1;
            else if (d1 < d2) return -1;
            d1 = d2 = 0;
            ++i; ++j;
        }
        return 0;
    }
};

由于这道题我们需要将版本号以'.'分开,那么我们可以借用强大的字符串流stringstream的功能来实现分段和转为整数,使用这种方法写的代码很简洁,如下所示:

解法三:

class Solution {
public:
    int compareVersion(string version1, string version2) {
        istringstream v1(version1 + "."), v2(version2 + ".");
        int d1 = 0, d2 = 0;
        char dot = '.';
        while (v1.good() || v2.good()) {
            if (v1.good()) v1 >> d1 >> dot;
            if (v2.good()) v2 >> d2 >> dot;
            if (d1 > d2) return 1;
            else if (d1 < d2) return -1;
            d1 = d2 = 0;
        }
        return 0;
    }
};

最后我们来看一种用C语言的字符串指针来实现的方法,这个方法的关键是用到将字符串转为长整型的strtol函数,关于此函数的用法可以参见我的另一篇博客strtol 函数用法。参见代码如下:

解法四:

class Solution {
public:
    int compareVersion(string version1, string version2) {
        int res = 0;
        char *v1 = (char*)version1.c_str(), *v2 = (char*)version2.c_str();
        while (res == 0 && (*v1 != '\0' || *v2 != '\0')) {
            long d1 = *v1 == '\0' ? 0 : strtol(v1, &v1, 10);
            long d2 = *v2 == '\0' ? 0 : strtol(v2, &v2, 10);
            if (d1 > d2) return 1;
            else if (d1 < d2) return -1;
            else {
                if (*v1 != '\0') ++v1;
                if (*v2 != '\0') ++v2;
            }
        }
        return res;
    }
};

类似题目:

First Bad Version

参考资料:

https://leetcode.com/problems/compare-version-numbers/discuss/?orderBy=most_votes

https://leetcode.com/problems/compare-version-numbers/discuss/50774/Accepted-small-Java-solution.

https://leetcode.com/problems/compare-version-numbers/discuss/50788/My-JAVA-solution-without-split

https://leetcode.com/problems/compare-version-numbers/discuss/50804/10-line-concise-solution.-(C%2B%2B)

https://leetcode.com/problems/compare-version-numbers/discuss/50767/My-2ms-easy-solution-with-CC%2B%2B

到此这篇关于C++实现LeetCode165.版本比较)的文章就介绍到这了,更多相关C++实现版本比较内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C语言实现像素鸟游戏

    C语言实现像素鸟游戏

    这篇文章主要为大家详细介绍了C语言实现像素鸟游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • C++深入浅出讲解隐藏this指针的用法

    C++深入浅出讲解隐藏this指针的用法

    在C++中,每一个对象都能通过this指针来访问自己的地址。this 指针是所有成员函数的隐含参数。因此,在成员函数内部,它可以用来指向调用对象
    2022-05-05
  • C++中构造函数的参数缺省的详解

    C++中构造函数的参数缺省的详解

    这篇文章主要介绍了C++中构造函数的参数缺省的详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-10-10
  • C/C++实现线性顺序表的示例代码

    C/C++实现线性顺序表的示例代码

    使用顺序存储结构的线性存储结构的表为线性顺序表。本文将分别利用C语言和C++实现线性顺序表,文中示例代码讲解详细,需要的可以参考一下
    2022-05-05
  • C语言中结构体、联合体的成员内存对齐情况

    C语言中结构体、联合体的成员内存对齐情况

    这篇文章主要给大家介绍了关于C语言中结构体、联合体的成员内存对齐情况的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • 基于一致性hash算法 C++语言的实现详解

    基于一致性hash算法 C++语言的实现详解

    在《基于一致性hash算法(consistent hashing)的使用详解》一文中已经介绍了一致性hash的基本原理,本文将会对其具体实现细节进行描述,并用c++语言对一致性hash进行了简单的实现
    2013-05-05
  • ubuntu中打开终端的三种解决方法

    ubuntu中打开终端的三种解决方法

    本篇文章是对ubuntu中打开终端的三种方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C语言练习之扫雷小游戏

    C语言练习之扫雷小游戏

    这篇文章主要为大家详细介绍了C语言练习之扫雷小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • C++ Boost Array与Unordered使用介绍

    C++ Boost Array与Unordered使用介绍

    Boost是为C++语言标准库提供扩展的一些C++程序库的总称。Boost库是一个可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一,是为C++语言标准库提供扩展的一些C++程序库的总称
    2022-11-11
  • C语言打印华氏-摄氏温度对照表的方法

    C语言打印华氏-摄氏温度对照表的方法

    这篇文章主要介绍了C语言打印华氏-摄氏温度对照表的方法,涉及C语言字符串与数字操作的相关技巧,非常简单实用,需要的朋友可以参考下
    2015-07-07

最新评论