[LC] 165. Compare Version Numbers
Compare two version numbers version1 and version2.
If version1 > version2
return 1;
if version1 < version2
return -1;
otherwise return 0
.
You may assume that the version strings are non-empty and contain only digits and the .
character.
The .
character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5
is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
You may assume the default revision number for each level of a version number to be 0
. For example, version number 3.4
has a revision number of 3
and 4
for its first and second level revision number. Its third and fourth level revision number are both 0
.
Example 1:
Input:version1
= "0.1",version2
= "1.1"
Output: -1
Example 2:
Input:version1
= "1.0.1",version2
= "1"
Output: 1
Example 3:
Input:version1
= "7.5.2.4",version2
= "7.5.3"
Output: -1
Example 4:
Input:version1
= "1.01",version2
= "1.001"
Output: 0
Explanation: Ignoring leading zeroes, both “01” and “001" represent the same number “1”
Example 5:
Input:version1
= "1.0",version2
= "1.0.0"
Output: 0
Explanation: The first version number does not have a third level revision number, which means its third level revision number is default to "0"
Solution 1:
class Solution {
public int compareVersion(String version1, String version2) {
if (version1 == null || version2 == null) {
return 0;
}
String[] strArr1 = version1.split("\\.");
String[] strArr2 = version2.split("\\.");
int index = 0;
while (index < strArr1.length && index < strArr2.length) {
int cur_str1 = Integer.parseInt(strArr1[index]);
int cur_str2 = Integer.parseInt(strArr2[index]);
if (cur_str1 < cur_str2) {
return -1;
} else if (cur_str1 > cur_str2) {
return 1;
}
index += 1;
} if (index < strArr1.length) {
for (int i = index; i < strArr1.length; i++) {
if (Integer.parseInt(strArr1[i]) > 0) {
return 1;
}
}
}
if (index < strArr2.length) {
for (int j = index; j < strArr2.length; j++) {
if (Integer.parseInt(strArr2[j]) > 0) {
return -1;
}
}
}
return 0;
}
}
Solution 2:
class Solution {
public int compareVersion(String version1, String version2) {
String[] strArr1 = version1.split("\\.");
String[] strArr2 = version2.split("\\.");
int len = Math.max(strArr1.length, strArr2.length);
for (int i = 0; i< len; i++) {
int cur_str1 = i >= strArr1.length ? 0 : Integer.parseInt(strArr1[i]);
int cur_str2 = i >= strArr2.length ? 0 : Integer.parseInt(strArr2[i]);
if (cur_str1 < cur_str2) {
return -1;
} else if (cur_str1 > cur_str2) {
return 1;
}
}
return 0;
}
}
[LC] 165. Compare Version Numbers的更多相关文章
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 165. Compare Version Numbers - LeetCode
Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...
- 【刷题-LeetCode】165 Compare Version Numbers
Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...
- 165. Compare Version Numbers比较版本号的大小
[抄题]: Compare two version numbers version1 and version2.If version1 > version2 return 1; if versi ...
- ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Java for LeetCode 165 Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- 【LeetCode】165 - Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Java [Leetcode 165]Compare Version Numbers
题目描述: Compare two version numbers version1 and version2.If version1 > version2 return 1, if versi ...
- 165. Compare Version Numbers
题目: Compare two version numbers version1 and version2.If version1 > version2 return 1, if version ...
随机推荐
- 关于js返回上一页的实现方法
以前在提交表单的时候,如果提交出错返回的时候信息内容全没了,我不知道要怎么保存,就开始了那种最愚蠢的做法,将填写的数据设置到session中,让后取出来用,不过没有试成功,总是有错,无意之中在我那本j ...
- php错误和异常的重定向
通过重定向错误或异常,我们可以更安全的显示错误信息,一般也用来记录错误和异常日志. 参数可以是全局函数名,也可以是类中的方法,非静态方法通过数组传递类名和方法名进去, 静态方法直接带命名空间和类名,看 ...
- Linux下自由切换用户
切换用户的命令是su,su是(switch user)切换用户的缩写.通过su命令,可以从普通用户切换到root用户,也可以从root用户切换到普通用户. 上述图中是linux下的终端页面,其中pyv ...
- OutOfMemoryError异常
1.Java堆溢出 Java堆用于存储对象实例,只要不断地创建对象,并且保证GC Roots到对象之间有可达路径来避免垃圾回收机制清除这些对象,就会在对象数量达到最大堆的容量限制后产生内存溢出异常. ...
- Linux 安装python3.x步骤
本文转发自博客园非真的文章,内容略有改动 本文已收录至博客专栏linux安装各种软件及配置环境教程中 linux系统本身默认安装有2.x版本的python,版本x根据不同版本系统有所不同,通过pyth ...
- Django框架(六):模型(二) 字段查询、查询集
1. 字段查询 通过模型类.objects属性可以调用如下函数,实现对模型类对应的数据表的查询. 函数名 功能 返回值 说明 get 返回表中满足条件的一条且只能有一条数据. 返回值是一个模型类对象. ...
- cocoaPods安装使用亲体验
一. cocoaPods的安装. 终端中输入: $ sudo gem install cocoapods 注意:直接在terminal中输入这个是安装不成功的,因此,我们可以通过淘宝的Ruby镜像来访 ...
- bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL ...
- MongoDB 索引 .explain("executionStats")
MongoDB干货系列2-MongoDB执行计划分析详解(3) http://www.mongoing.com/eshu_explain3 MongoDB之使用explain和hint性能分析和优化 ...
- dubbo通信协议对比
对dubbo的协议的学习,可以知道目前主流RPC通信大概是什么情况,本文参考dubbo官方文档 http://dubbo.io/User+Guide-zh.htm dubbo共支持如下几种通信协议: ...