Java for LeetCode 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
解题思路:
先进行split,然后转为int比较即可,JAVA实现如下:
static public int compareVersion(String version1, String version2) {
String[] s1=version1.split("\\.");
String[] s2=version2.split("\\.");
int[] num1=new int[Math.max(s1.length, s2.length)];
int[] num2=new int[Math.max(s1.length, s2.length)];
for(int i=0;i<s1.length;i++)
num1[i]= Integer.valueOf(s1[i]);
for(int i=0;i<s2.length;i++)
num2[i]= Integer.valueOf(s2[i]);
for(int i=0;i<num1.length;i++){
if(num1[i]>num2[i])
return 1;
else if(num1[i]<num2[i])
return -1;
}
return 0;
}
Java for LeetCode 165 Compare Version Numbers的更多相关文章
- ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java
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 ...
- [LeetCode] 165. Compare Version Numbers 比较版本数
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- Leetcode 165 Compare Version Numbers
题意:比较版本号的大小 有点变态,容易犯错 本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号 class Solution { public: int c ...
- 【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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【LeetCode】165 - Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- 【CodeForces 602B】G - 一般水的题2-Approximating a Constant Range
Description When Xellos was doing a practice course in university, he once had to measure the intens ...
- 【CodeForces 602A】C - 特别水的题3-Two Bases
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102271#problem/C Description After seeing the ...
- js阻止表单重复提交
//校验表单的数据 function newFatherModuleVerify() { var moduelName = $('#fatherModule_moduelName').val(); a ...
- hihoCoder 1195 高斯消元.一
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:喂不得了啦,那边便利店的薯片半价了! 小Hi:啥?! 小Ho:那边的便利店在打折促销啊. 小Hi:走走走, ...
- easyui datagrid 编辑器绑定事件
依照网上的和自己想的,在获取编辑器后直接绑定事件,思路没有问题,但是总是不响应 细细浏览网上的资料,无意中看到editor 的type 类型和自己写的不一致,自己写的是textbox,而网上的是val ...
- hdu 2085 核反应堆
看完题,想到用结构体存储高质点和低质点,然后打表存储<33的质点数量. #include<stdio.h> struct hilo { long long hi,lo; }; int ...
- linux+apache url大小写敏感问题
Linux对文件目录大小写敏感,URL大小写敏感会导致网页打不开,解决方法之一是启用Apache的mod_speling.so模块. 1.确认/usr/lib/httpd/modules目录下是否存在 ...
- phpcms 网站迁移服务器
相信很多人不知道怎么去把PHPCMS V9进行搬家 在本地测试好的phpcms v9网站需要搬到服务器上,可以用以下方法: 1.上传所有的程序文件(如果主机支持压缩包在线解压,那么就打成zip的包,f ...
- 6种编写HTML和CSS的最有效的方法
感谢HTML5和CSS3,以及JavaScript,前端开发者有了大大的用武之地.大家都在用很多的工具和技术来武装自己,以加快前段的开发. 本文分享了6中最有效的方法,希望能提供你的效率,为你节约时间 ...
- 记录一次centos6.4版本的VSFTP本地用户登陆的配置
其实vsftp是一个非常常用而且简单的服务,但是假如服务不是你配置的前者没有留下参考档案,的确是件头疼的事儿,特此记录下. 首先是vsftp的安装当然安装有源码的编译和yum等 这里我选择rpm包的y ...