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. FPGA学习心得汇总(手中写代码,心中有电路)

    http://bbs.ednchina.com/BLOG_ARTICLE_2111172.HTM 任何的时序逻辑都可以转换为组合逻辑+D触发器来完成. FPGA内部主要三块:可编程的逻辑单元.可编程的 ...

  2. JDBC13 ORM02 Map封装

    用Map封装一条信息 conn=Utils.getConn(); ps=conn.prepareStatement("select Empname,birthday,salary from ...

  3. [hdu3507 Print Article]斜率优化dp入门

    题意:需要打印n个正整数,1个数要么单独打印要么和前面一个数一起打印,1次打印1组数的代价为这组数的和的平方加上常数M.求最小代价. 思路:如果令dp[i]为打印前i个数的最小代价,那么有 dp[i] ...

  4. Nginx下的location,upstream,rewrite 和 proxy_pass使用总计大全

    一 . location: 顾名思义-->地址,也叫路由. nginx服务器非常核心的配置,一般nginx运维人员在修改nginx配置时,大部分也是围绕着location这个配置进行修改. 下面 ...

  5. AndroidStudio3.6升级后的坑-apk打包

    前段时间尝试了最新版的AndroidStudio3.6,整体来说gradle调试和自带的虚拟机相比较历史版本有了更香的体验. 刚好有个新项目,就直接使用最新版了,这次新版的升级除了保持原有的界面风格, ...

  6. 数据库当中删除数据后主键id不连续的问题

    新建查询: ALTER TABLE `表名` DROP `主键名`;ALTER TABLE `表名` ADD `主键名` int NOT NULL FIRST;ALTER TABLE `表名` MOD ...

  7. Python内置函数示例

    abs() 返回数字绝对值 >>> abs(-100) 100 >>> abs(10) 10 >>> all() 判断给定的可迭代参数 itera ...

  8. 【Jenkins学习】【第二节】 jenkins构建触发器定时任务

    一.定时构建 Build periodically:定时执行构建任务,不管远程代码分支上的代码是否发生变化,都执行一次构建. 语法:* * * * *(五颗星,中间用空格隔开) 第一个:分钟,取值0~ ...

  9. Java基础之数据类型

    一.数据类型 基本数据类型介绍 byte 1字节 char 2字节 short 2字节 int 4字节 long 8字节 float 4字节 double 8字节 以上有Java中八大基本类型的7种, ...

  10. 51Nod - 1255

    也是第十一届校赛的C题,不过他把1e5改成了1e7. 一开始就想到用贪心做.思路是这样的:开一个字符数组ans保存答案.然后从头到尾遍历题目给出的字符串S,如果ans数组中还没有这个字母,那么就把字母 ...