【leetcode】Compare Version Numbers(middle)

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.

Here is an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37

思路:

以 . 为分隔符分割数字,依次对比大小。注意两个版本号长度不一样的情况。

int compareVersion(string version1, string version2) {
        int i = 0, j = 0;
        int n1 = 0, n2 = 0;
        while(i < version1.size() && j < version2.size()) //对比每一个小数点前对应的数字
        {
            n1 = 0, n2 = 0;
            while(i < version1.size() && version1[i++] != ‘.‘)
                n1 = n1 * 10 + version1[i - 1] - ‘0‘;
            while(j < version2.size() && version2[j++] != ‘.‘)
                n2 = n2 * 10 + version2[j - 1] - ‘0‘;

            if(n1 > n2) return 1;
            if(n1 < n2) return -1;
        }
        //处理数字数量不一样多的情况 如 1.0 和 1 或 1.0.0.4 和 1.0  此时肯定比较短的那个版本号已经到头了 只要获取剩下的那个版本号后面的数字是否有大于0的即可
        n1 = 0, n2 = 0;
        while(i++ < version1.size())
            n1 = (version1[i - 1] == ‘.‘) ? n1 : n1 * 10 + version1[i - 1] - ‘0‘;
        while(j++ < version2.size())
            n2 = (version2[j - 1] == ‘.‘) ? n2 : n2 * 10 + version2[j - 1] - ‘0‘;
        if(n1 > n2) return 1;
        else if(n1 < n2) return -1;
        else return 0;

    }

大神的代码,简洁很多。相当于把我的代码下面的循环部分和上面的融合在一起了。

public class Solution {
    public int compareVersion(String version1, String version2) {
        String[] v1 = version1.split("\\.");
        String[] v2 = version2.split("\\.");

        int longest = v1.length > v2.length? v1.length: v2.length;

        for(int i=0; i<longest; i++)
        {
            int ver1 = i<v1.length? Integer.parseInt(v1[i]): 0;
            int ver2 = i<v2.length? Integer.parseInt(v2[i]): 0;

            if(ver1> ver2) return 1;
            if(ver1 < ver2) return -1;
        }
        return 0;
    }
}
时间: 2024-10-07 11:48:47

【leetcode】Compare Version Numbers(middle)的相关文章

【leetcode】Compare Version Numbers

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 . 

【leetcode】Combination Sum III(middle)

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

【leetcode】Divide Two Integers (middle)☆

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路: 尼玛,各种通不过,开始用纯减法,超时了. 然后用递归,溢出了. 再然后终于开窍了,用循环,把被除数每次加倍去找答案,结果一遇到 -2147483648 就各种不行, 主要是这个数一求绝对值就溢出了. 再然后,受不了了,看答案. 发现,大家都用long long来解决溢

【leetcode】Insertion Sort List (middle)

Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分完全分开,指针不要有交叉. 即不会通过->next 重叠 class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head == NULL) return NULL; ListNode * ans = hea

【leetcode】Repeated DNA Sequences(middle)★

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Write a function to find all the 10-letter-long seq

【leetcode】Set Matrix Zeroes(middle)

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用额外空间,就用矩阵的第一行和第一列来标记这一行或这一列是否需要置0. 用两个bool量记录第一行和第一列是否需要置0 大神的代码和我的代码都是这个思路,但是我在画0的时候是行列分开处理的,大神的代码是一起处理的 void setZeroes(vector<vector<int> > &

【leetcode】Balanced Binary Tree(middle)

Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 思路: 我居然在这道题上卡了一个小时.关键是对于平衡的定义,我开始理解

【LeetCode】数组--合并区间(56)

写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组(Array)是一种线性表数据结构.它用一组连续的内存空间,来存储一组具有相同类型的数据.在每一种编程语言中,基本都会有数组这种数据类型.不过,它不仅仅是一种编程语言中的数据类型,还是一种最基础的数据结构. 贪心算法回顾: [LeetCode]贪心算法--买卖股票的最佳时机II(122) [LeetC

leetcode_165题——Compare Version Numbers(string)

Compare Version Numbers Total Accepted: 19548 Total Submissions: 130867My Submissions Question Solution Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may