zoukankan      html  css  js  c++  java
  • [LeetCode] 165. Compare Version Numbers

    Given two version numbers, version1 and version2, compare them.

    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.

    Example 1:

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

    Example 2:

    Input: version1 = "1.0", version2 = "1.0.0"
    Output: 0
    Explanation: version1 does not specify revision 2, which means it is treated as "0".
    

    Example 3:

    Input: version1 = "0.1", version2 = "1.1"
    Output: -1
    Explanation: version1's revision 0 is "0", while version2's revision 0 is "1". 0 < 1, so version1 < version2.
    

    Example 4:

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

    Example 5:

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

    Constraints:

    • 1 <= version1.length, version2.length <= 500
    • version1 and version2 only contain digits and '.'.
    • version1 and version2 are valid version numbers.
    • All the given revisions in version1 and version2 can be stored in a 32-bit integer.

    比较版本号。

    给你两个版本号 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。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/compare-version-numbers
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    题意是给两个以字符串展示的版本号,请比较他们的大小。比较的规则很直白,这里考察的应该是你如何把 char 转变成 int。Java 和 JavaScript 都有一个叫做 parseInt 的函数来帮助完成这个动作。

    时间O(max(m, n))

    空间O(1)

    Java实现

     1 class Solution {
     2     public int compareVersion(String version1, String version2) {
     3         String[] v1 = version1.split("\.");
     4         String[] v2 = version2.split("\.");
     5         for (int i = 0; i < Math.max(v1.length, v2.length); i++) {
     6             int num1 = i < v1.length ? Integer.parseInt(v1[i]) : 0;
     7             int num2 = i < v2.length ? Integer.parseInt(v2[i]) : 0;
     8             if (num1 < num2) {
     9                 return -1;
    10             } else if (num1 > num2) {
    11                 return 1;
    12             }
    13         }
    14         return 0;
    15     }
    16 }

    JavaScript实现

     1 /**
     2  * @param {string} version1
     3  * @param {string} version2
     4  * @return {number}
     5  */
     6 var compareVersion = function (version1, version2) {
     7     let v1 = version1.split('.');
     8     let v2 = version2.split('.');
     9     let len = Math.max(v1.length, v2.length);
    10     for (let i = 0; i < len; i++) {
    11         let num1 = i < v1.length ? parseInt(v1[i]) : 0;
    12         let num2 = i < v2.length ? parseInt(v2[i]) : 0;
    13         if (num1 < num2) {
    14             return -1;
    15         } else if (num1 > num2) {
    16             return 1;
    17         }
    18     }
    19     return 0;
    20 };

    LeetCode 题目总结

  • 相关阅读:
    put 创建索引
    post创建一个空索引
    elasticsearch 安装marvel
    elasticsearch 安装bigdesk插件
    perl 播放windows文件
    如何在十分钟内插入1亿条记录到Oracle数据库?
    常见地址说明
    如何做出实用而强大的数据地图?
    如何利用jqGrid表格方法重新设置caption属性值
    如何给jqGrid表格按照字段进行分组
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13185588.html
Copyright © 2011-2022 走看看