[LeetCode]165.Compare Version Numbers

【题目】

Compare two version numbers version1 and version1.
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.

Here is an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37

【分析】

类似于split的方法把字符串解析, 然后再比较。

(1)将两个字符串version1和version2进行分割,因为可能会出现这样的测试用例"1.0.1",有多个点。

(2)容器vector存储按照"."分割之后的数字。

(3)然后依次进行比较。

【代码】

    /**------------------------------------
    *   日期:2015-02-02
    *   作者:SJF0115
    *   题目: 165.Compare Version Numbers
    *   网址:https://oj.leetcode.com/problems/compare-version-numbers/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            int size1 = version1.size();
            int size2 = version2.size();
            vector<int> v1;
            vector<int> v2;
            // 解析version1
            int sum = 0;
            for(int i = 0;i < size1;++i){
                if(version1[i] == '.'){
                    v1.push_back(sum);
                    sum = 0;
                }//if
                else{
                    sum = sum * 10 + version1[i] - '0';
                }//else
            }//for
            v1.push_back(sum);
            // 解析version2
            sum = 0;
            for(int i = 0;i < size2;++i){
                if(version2[i] == '.'){
                    v2.push_back(sum);
                    sum = 0;
                }//if
                else{
                    sum = sum * 10 + version2[i] - '0';
                }//else
            }//for
            v2.push_back(sum);
            // 比较
            int count1 = v1.size();
            int count2 = v2.size();
            int num1,num2;
            for(int i = 0,j = 0;i < count1 || j < count2;++i,++j){
                num1 = i < count1 ? v1[i] : 0;
                num2 = j < count2 ? v2[j] : 0;
                if(num1 > num2){
                    return 1;
                }//if
                else if(num1 < num2){
                    return -1;
                }//else
            }//for
            return 0;
        }
    };

    int main(){
        Solution s;
        string str1("1.1");
        //string str1("13.27.24");
        string str2("1.1");
        int result = s.compareVersion(str1,str2);
        // 输出
        cout<<result<<endl;
        return 0;
    }

【思路二】

思路二是对思路一的优化,思路一中,必须把version1,version2都解析完全才能比较。

例如:version1 = "13.27.23"  version2 = "13.28.25"

思路一中23和25都会遍历到,这其实对结果没有什么影响了,因为前面的27和28已经决定了哪个版本的大小了,因此我们可以不必要解析后面的字符串。

基于这样的思路我们边解析边比较,一旦有结果便停止解析。

【代码二】

    /**------------------------------------
    *   日期:2015-02-02
    *   作者:SJF0115
    *   题目: 165.Compare Version Numbers
    *   网址:https://oj.leetcode.com/problems/compare-version-numbers/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/

    class Solution {
    public:
        int compareVersion(string version1, string version2) {
            int size1 = version1.size();
            int size2 = version2.size();
            // 解析version
            int sum1,sum2,i,j;
            for(i = 0,j = 0;i < size1 || j < size2;++i,++j){
                // version1
                sum1 = 0;
                while(i < size1 && version1[i] != '.'){
                    sum1 = sum1 * 10 + version1[i] - '0';
                    ++i;
                }//while
                // version2
                sum2 = 0;
                while(j < size2 && version2[j] != '.'){
                    sum2 = sum2 * 10 + version2[j] - '0';
                    ++j;
                }//while
                // compare
                if(sum1 > sum2){
                    return 1;
                }//if
                else if(sum1 < sum2){
                    return -1;
                }//else
            }//for
            return 0;
        }
    };

时间: 2024-09-04 05:00:48

[LeetCode]165.Compare Version Numbers的相关文章

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

leetcode 002 add two numbers C语言

问题描述 leetcode 002 add two numbers C语言 struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { struct ListNode *l, *p; l = (struct ListNode*)malloc(sizeof(struct ListNode)); l->val = l1->val + l2->val; p = l; l1 = l1->next; l

[LeetCode] Sum of Square Numbers 平方数之和

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5  Example 2: Input: 3 Output: False 这道题让我们求一个数是否能由平方数之和组成,刚开始博主没仔细看题,没有

LeetCode 2 Add Two Numbers

翻译: 给你两个表示两个非负数字的链表.数字以相反的顺序存储,其节点包含单个数字.将这两个数字相加并将其作为一个链表返回. 输入: (2 -> 4 -> 3) + (5 -> 6 -> 4) 输出: 7 -> 0 -> 8 原题: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of

[LeetCode] Bitwise AND of Numbers Range

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 解题思路 ① n&(n-1),可以去除n的最低位的1. ② 从n一直与到m,可以去掉的1就是n和m的右端不相等的部分的1. 例如对于

[LeetCode] First Bad Version - 二分查找

题目概述:You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a b

[LeetCode] First Bad Version

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad ve

Java中split以&amp;#183;点分割的问题

[LeetCode]–165. Compare Version Numbers这个问题中,关于String的split(".")不能切分的问题. 今天开发中使用字符串分割函数split(),发现: String s = "upload/20120416135915265.sql"; System.out.println(s.split(".")); 输出的并不是想要的结果,之后输出: System.out.println(s.split(&quo

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能