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. 51单片机实战UcosII操作系统

    中断定义为CPU对系统内外发生的异步事件的响应.异步事件是指没有一定时序关系的.随机发生的事件. 与前后台系统中的中断服务子程序不同,uC/OS-Ⅱ要知道当前内核是否正在处理中断.是否脱离中断. OS ...

  2. dp规划之矩阵连乘问题

    问题描述:给定n个矩阵{A1,A2,...,An},其中Ai与Ai+1是可乘的,我们要计算这n个矩阵的最少计算次数.  将多个矩阵连乘抽象为最后一次计算即两个矩阵相乘. public static v ...

  3. [csu/coj 1080]划分树求区间前k大数和

    题意:从某个区间内最多选择k个数,使得和最大 思路:首先题目给定的数有负数,如果区间前k大出现负数,那么负数不选和更大,于是对于所有最优选择,负数不会出现,所以用0取代负数,问题便转化为区间的前k大数 ...

  4. 关于jquery样式切换的一些想法

    前一阵子写了一些代码,都是关于一个按钮点击切换状态的按钮,当时没有想周到就用addClass removeClass来控制这个控件的状态,后来想想觉得不妥. <html> <head ...

  5. java8 新特性Stream流的应用

    作为一个合格的程序员,如何让代码更简洁明了,提升编码速度尼. 今天跟着我一起来学习下java 8  stream 流的应用吧. 废话不多说,直入正题. 考虑以下业务场景,有四个人员信息,我们需要根据性 ...

  6. zabbix配置主动式监控的步骤(原创)

    步骤如下: 1.克隆模板.命名新的模板名,并点击"监控项",全选,批量更新时第一个“类型”打勾,客户端改为主动式: 2.添加客户端或更改原有的模板为新模板(服务器端添加客户端时的配 ...

  7. iterm 分屏切换快捷键与配色设置

    (1)快捷键设置 ⌘ + d: 垂直分屏, ⌘ + shift + d: 水平分屏. ⌘ + ]和⌘ + [在最近使用的分屏直接切换. ⌘ + opt + 方向键切换到指定位置的分屏. ⌘ + 数字: ...

  8. 全局设置UITableView的属性|正确计算contentSize|MJRefresh mj_footer 能正常隐藏在底部,不因为数据过少展示在页面中部

    可在AppDelegate中设置 if (@available(iOS 11.0, *)) { UITableView.appearance.estimatedRowHeight = 0; UITab ...

  9. 树形DP基础题 HDU1520

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. wordpress获取当前分类的子分类

    1.现在function.php里面添加下面的代码 function get_category_root_id($cat) { $this_category = get_category($cat); ...