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. 利用Asp.net和Sql Server实现留言板功能

    本教程设及到:使用SQL Server查询分析器创建数据库:SQL查询语句常用的一些属性值:触发器创建和使用:存储过程的创建,ASP使用存储过程. 正文: 一.创建数据库: 创建一个feedback数 ...

  2. etcd实现服务发现

    前言 etcd环境安装与使用文章中介绍了etcd的安装及v3 API使用,本篇将介绍如何使用etcd实现服务发现功能. 服务发现介绍 服务发现要解决的也是分布式系统中最常见的问题之一,即在同一个分布式 ...

  3. linux --文件目录的学习

    https://www.runoob.com/linux/linux-file-content-manage.html /boot:存放的启动Linux 时使用的内核文件,包括连接文件以及镜像文件. ...

  4. react 学习前期用到的插件

    prop-types------展示组件的props类型检测: import PropTypes from 'prop-types' ... Link.propTypes = { active: Pr ...

  5. 输入一个整数n,输出契波那契数列的第n项

    package bianchengti; /* * 输入一个整数n,输出契波那契数列的第n项 * 斐波那契数列:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89... */ p ...

  6. linux文档目录

  7. redis python操作

    1.基于连接池方式实现对五个数据类型操作,每种数据类型2个操作 2.基于spring-data-redis 基于jedis来实现对五种数据类型操作,每种数据类型实现两个操作,包括事务 以上为基于jav ...

  8. MySQL 5.7 基于GTID创建运行主库的从库-xtrabackup+mysqldump

    一.GTID innobackupex备份实现主从同步 1)master备份 innobackupex --defaults-file=/etc/my.cnf --user=root --passwo ...

  9. POJ1015

    题目链接:http://poj.org/problem?id=1015 大概题意: 法庭要挑选m人陪审团.先随机挑选n个公民,对于每个公民,控辩双方都有各自的“喜好度”p[ ] 和 d[ ],法庭要尽 ...

  10. 如何在Teamcenter中使用PMI?

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