Compare Version Numbers
Compare two version numbers version1 and version1.
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
本题特殊情况要考虑到。v1 和v1.0 的比较。
- public class Solution {
- public int compareVersion(String version1, String version2) {
- String[] splits1=version1.split("\\.",-1);
- String[] splits2=version2.split("\\.",-1);
- int shortLength=Math.min(splits1.length,splits2.length);
- int longLength=Math.max(splits2.length,splits1.length);
- int i=0;
- for(i=0;i<shortLength;i++)
- {
- if(Integer.parseInt(splits1[i])<Integer.parseInt(splits2[i]))
- {
- return -1;
- }
- else if(Integer.parseInt(splits1[i])>Integer.parseInt(splits2[i]))
- {
- return 1;
- }
- }
- if(i==longLength)
- {
- return 0;
- }
- else
- {
- while(i<longLength)//如果两个版本号不同长度,但是是同一版本的情况,如v1.0 和v1
- {
- if(splits1.length==longLength)
- {
- if(Integer.parseInt(splits1[i])!=0)
- {
- return 1;
- }
- }
- else if(splits2.length==longLength)
- {
- if(Integer.parseInt(splits2[i])!=0)
- {
- return -1;
- }
- }
- i++;
- }
- return 0;
- }
- }
- }
Compare Version Numbers的更多相关文章
- 2016.5.21——Compare Version Numbers
Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A' ) 2.srt.at(),substr ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【刷题-LeetCode】165 Compare Version Numbers
Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...
- 165. Compare Version Numbers - LeetCode
Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- 【leetcode】Compare Version Numbers
题目描述: Compare two version numbers version1 and version2. If version1 > version2 return 1, if vers ...
- 【leetcode】Compare Version Numbers(middle)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- asp.net读取execl模板并填充数据,关闭进程
<head runat="server"> <title></title> <script src="Scripts/jquer ...
- JS复习--更新结束
js复习-01---03 一 JS简介 1,文档对象模型 2,浏览器对象模型 二 在HTML中使用JS 1,在html中使用<script></script>标签 2,引入外部 ...
- IBatis 批量插入数据之SqlBulkCopy
public void AddLetters(IList<int> customerIds, string title, string content, LetterEnum.Letter ...
- 主流ORM对比分析,莫人云亦云
目前主流的ORM框架有Entity Framework,Dapper,NHibernate,NBear,Castle ActiveRecord,BATIS.NET六种,都是免费开源的.下边从官方支持性 ...
- dos 固定ip命令
dos 固定ip命令 ***************************************************************************************** ...
- centos 下测试网速
wget https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py chmod a+rx speedtest. ...
- C# 异步
private void GetHttpResponse() { var client = new Microsoft.HBase.Client.HBaseClient(new ClusterCred ...
- 手动封装js原生XMLHttprequest异步请求
Code Object.extend =function(targetObj,fnJson){ //扩展方法,类似于jQuery的$.extend,可以扩展类的方法,也可以合并对象 for(var f ...
- 第2月第5天 arc invocation getReturnValue
http://blog.csdn.net/zengconggen/article/details/38024625
- CSS3——transform学习
CSS动画效果可以使用transform和Animation,前者较简单,先学习前者. transform有几个基本变换,平移.旋转.缩放.扭曲 一.translate平移 有translate2d和 ...