165. 比较版本号

比较两个版本号 version1 和 version2。

如果 version1 > version2 返回 1,如果 version1 < version2 返回 -1, 除此之外返回 0。

你可以假设版本字符串非空,并且只包含数字和 . 字符。

. 字符不代表小数点,而是用于分隔数字序列。

例如,2.5 不是“两个半”,也不是“差一半到三”,而是第二版中的第五个小版本。

你可以假设版本号的每一级的默认修订版号为 0。例如,版本号 3.4 的第一级(大版本)和第二级(小版本)修订号分别为 3 和 4。其第三级和第四级修订号均为 0。

示例 1:

输入: version1 = “0.1”, version2 = “1.1”

输出: -1

示例 2:

输入: version1 = “1.0.1”, version2 = “1”

输出: 1

示例 3:

输入: version1 = “7.5.2.4”, version2 = “7.5.3”

输出: -1

示例 4:

输入:version1 = “1.01”, version2 = “1.001”

输出:0

解释:忽略前导零,“01” 和 “001” 表示相同的数字 “1”。

示例 5:

输入:version1 = “1.0”, version2 = “1.0.0”

输出:0

解释:version1 没有第三级修订号,这意味着它的第三级修订号默认为 “0”。

提示:

版本字符串由以点 (.) 分隔的数字字符串组成。这个数字字符串可能有前导零。

版本字符串不以点开始或结束,并且其中不会有两个连续的点。

class Solution {
public int compareVersion(String version1, String version2) {
String[] a1 = version1.split("\\.");
String[] a2 = version2.split("\\."); for(int n = 0; n < Math.max(a1.length, a2.length); n++){
int i = (n < a1.length ? Integer.valueOf(a1[n]) : 0);
int j = (n < a2.length ? Integer.valueOf(a2[n]) : 0);
if(i < j) return -1;
else if(i > j) return 1;
}
return 0;
}
}

Java实现 LeetCode 165 比较版本号的更多相关文章

  1. Java for LeetCode 165 Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. C++内存管理学习笔记(2)

    /****************************************************************/ /*            学习是合作和分享式的! /* Auth ...

  2. 未联网下,在eclipse中编辑xml文件如何自动提示设置

    断网情况下,用eclipse编辑xml文件如何自动提示? 以编辑hibernate中的xml为例: 首先,我们都知道xml提示是引用.dtd文件的. 1.复制这个dtd路径,设置eclipse属性,搜 ...

  3. webpack的proxytable的配置

    这个一定不能忘记了/rest/后面的/,否则就是404找不到接口 这样的实现效果是 this.axios.post('/api/delShare', qs.stringify({'Id':Number ...

  4. 【C#】CsvHelper 使用手册

    目录 简介 模块 读取 读取所有记录 逐条读取 读取单个字段 写入 写入所有记录 逐条写入 逐字段写入 特性 Index Name NameIndex Ignore Optional Default ...

  5. Element Form表单实践(上)

    作者:小土豆biubiubiu 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.im/user/58c61b4361ff4b005d9e8 ...

  6. 3.2 Go整数类型

    1. Go整数类型 Go语言的数值类型包含不同大小的整数型.浮点数和负数,每种数值类型都有大小范围以及正负符号. 官方文档解释数据类型 int类型中哪些支持负数 有符号(负号):int8 int16 ...

  7. 【MySQL】如何解决分库分表遇到的自增主键的问题?

    雪花算法 Redis生成主键

  8. ShoneSharp语言(S#)的设计和使用介绍系列(8)— 最炫“公式”风

    ShoneSharp语言(S#)的设计和使用介绍 系列(8)— 最炫“公式”风 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ShoneSha ...

  9. .net core HttpClient 使用之消息管道解析(二)

    一.前言 前面分享了 .net core HttpClient 使用之掉坑解析(一),今天来分享自定义消息处理HttpMessageHandler和PrimaryHttpMessageHandler ...

  10. 如何在Teamcenter中使用PMI?

    1 .什么是PMI 在设计制造领域,PMI指的是产品制造信息(Productand Manufacturing Information),其目的在于在三维环境下,将制造信息从设计部门传递到制造部门.其 ...