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.
Here is an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
题目含义:版本号一般表示为 2.10,3.2.3这样的只含有数字和小数点的形式。比较两个版本号的大小。
需要注意的是,2.1代表2号大版本的第1个小版本,2.10代表2号大版本的第10个小版本,所以2.10比2.1大。同时,2.1.0和2.1代表的是同一个版本。
1 public int compareVersion(String version1, String version2) { 2 String[] versionOne = version1.split("\\."); 3 String[] versionTwo = version2.split("\\."); 4 int length = Math.max(versionOne.length,versionTwo.length); 5 for (int i=0;i<length;i++) 6 { 7 Integer value1 = i<versionOne.length?Integer.valueOf(versionOne[i]):0; 8 Integer value2 = i<versionTwo.length?Integer.valueOf(versionTwo[i]):0; 9 int result = value1.compareTo(value2); 10 if (value1.compareTo(value2) != 0) 11 { 12 return result; 13 } 14 } 15 return 0; 16 }
分类:
leetcode_string
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!