[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 ...
随机推荐
- 命令行的操作——cd
初学命令行,总是提示找不到有关的文件,当时的我啥也不知道,最后就很惨,啥也找不到 后来就只好自己上网找资料了 以下博文改编自小佳的博客 在需要处理不同路径下的文件的时候,经常要在命令行模式下切换目录, ...
- Java Properties基础知识总结
在Java语言中,使用一种以.properties为扩展名的文本文件作为资源文件,该类型的文件的内容格式为类似: some_key=some_value #注释描述 还有一种是使用xml文件保存项目的 ...
- 吴裕雄--天生自然 PYTHON3开发学习:元组
tup1 = ('Google', 'Runoob', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print ("tup1[0]: ", ...
- 蓝桥杯2015-省赛-C/C++-A组2题 星系炸弹
在X星系的广袤空间中漂浮着许多X星人造“炸弹”,用来作为宇宙中的路标.每个炸弹都可以设定多少天之后爆炸.比如:阿尔法炸弹2015年1月1日放置,定时为15天,则它在2015年1月16日爆炸.有一个贝塔 ...
- 图论模型--dijstra算法和floyd算法
matlab代码实现:https://blog.csdn.net/weixin_40108753/article/details/81237585 python代码实现:
- NOIP复赛文件路径怎么写
以2018年NOIP普及组复赛为例,四道题对应着四个文件夹: 随便选一道题,比如第一道题,进入title目录,可以看到title1.in, title1.ans, title2.in, title ...
- Office文档WEB端在线浏览(转换成Html)
最近在做了一个项目,要求是对Office文档在线预览.下面给大家分享一下我的方法. 1.第一种方法(不建议使用)我是在网上搜了一个利用COM组件对office文档进行转换,但是此方法必须要装Offic ...
- Microsoft.Office.Inter.Excel.dll在調用時可能會出現如下錯誤
Microsoft.Office.Inter.Excel.dll在調用時可能會出現如下錯誤,具體解決方案如下: 1. 錯誤資訊:檢索 COM 類工廠中 CLSID 為{00024500-0000-00 ...
- zabbix3.4--配置微信告警
1.注册企业微信 https://work.weixin.qq.com/ 2.注册好后登陆,点击“我的企业”,记录企业ID. 3.点击“应用管理”--“创建应用”,创建应用时添加接收告警的用户 4.添 ...
- python解一元一次方程
将未知数看成是虚数 将常数看成是实数 最终求解. import re class Item: def __init__(self,imag=0,real=0): self.imag = imag se ...