[leet code 165]Compare Version Numbers
1 题目
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
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
2 思路
好吧,这题很蛋疼,各种情况。有1.0与1比较的,有1.2.3与1.2比较的,考虑到各种情况,很复杂。还发现java里面split函数输入是正则表达式,要用"."符号分割的话,得这样.split"\\.",才能分割。
网上查了查,有一个思路是用递归解决的,http://www.tuicool.com/articles/3QV7NvV。完美解决 比较 1.0与1的问题。
我感觉实际操作中,会规定死版本的格式吧。如1.2.3与1.0.0。另外,我看实际中的一个代码,只要发现两个版本的字符串不一样,就提醒用户更新版本- -。
3 代码
public int compareVersion(String version1, String version2) {
if(version1.equals(version2))
return 0;
int fversion1 , fversion2;//最左边字符串代表的数字
String sversion1,sversion2;//剔除.号左边的数字剩下的字符串
if(version1.contains(".")){
int pos = version1.indexOf(".");
fversion1 = Integer.valueOf(version1.substring(0,pos));
sversion1 = version1.substring(pos+1,version1.length());
}else {
fversion1 = Integer.valueOf(version1);
sversion1 = "0";//预防比较1.0与1这种情况
}
if(version2.contains(".")){
int pos = version2.indexOf(".");
fversion2 = Integer.valueOf(version2.substring(0,pos));
sversion2 = version2.substring(pos+1,version2.length());
}else {
fversion2 = Integer.valueOf(version2);
sversion2 = "0";
}
if(fversion1 > fversion2)
return 1;
else if(fversion1 < fversion2)
return -1;
else return compareVersion(sversion1, sversion2);
}
[leet code 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 ...
- ✡ 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 ...
- 【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
随机推荐
- How to return AJAX errors from Laravel Controller?
Questions: I am building a REST API with Laravel 5. In Laravel 5, you can subclassApp\Http\Requests\ ...
- 跟我学Spring Boot(二)Hello World
1.打开DemoApplication添加如下代码 package com.example; import org.springframework.boot.SpringApplication; im ...
- Linux入门练习操作命令
查看目录命令 1. 显示目录下所有文件 2.显示所有文件,包括隐藏文件 创建目录命令 1.在改目录下创建文件夹“practise” 切换目录 1.切换到指定的目录 2.切换到上一级目录 3.还在当前目 ...
- openssl 连接 https(nginx)
参考源码路径 demos\ssl #include <stdio.h> #include <string.h> #include <stdlib.h> #incl ...
- .net获取本地ip地址
整理代码,.net获取本地ip地址,代码如下: string name = Dns.GetHostName(); IPHostEntry IpEntry = Dns.GetHostEntry(name ...
- oracle创建、删除 数据库、建立表空间以及插入 删除 修改表
一.创建.删除数据库 oracle OraDb11g_home->配置和移植工具->Database configration Assistant->...然后可以创建或者删除数据 ...
- Asp.Net 启用全局IE兼容模式
Asp.Net 启用全局IE兼容模式,不失为一个种简单最有效的解决方案: <system.webServer> <!-- 配置全局兼容 --> <httpProtocol ...
- SparkStreaming updateStateByKey 保存记录信息
)(_+_) ) 查看是否存在,如果存在直接获取 )) ssc.checkpoint() )) //使用updateStateByKey 来更新状态 val stateDstream = wordDs ...
- js网页上画图
保存 1.d3.js (http://www.d3.org/)使用svg技术,展示大数据量,动态效果很好,但是API暴露的不好,得靠自己摸索. 2.http://raphaeljs.com/refe ...
- PHP的设计模式之工厂模式
以前写代码老觉得,搞那么多乱七八槽的设计模式干嘛啊,这不是自己找罪受嘛.现在在这次的API开发过程中才晓得设计模式的厉害,真的是境界不到,永远不能领悟呀.还好坚持编码这么久,终于进入设计模式的运用了, ...