Java [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
解题思路:
以小数点分开,逐个比较。
代码如下:
public class Solution {
    public int compareVersion(String version1, String version2) {
    	String[] levels1 = version1.split("\\.");
    	String[] levels2 = version2.split("\\.");
    	int length = Math.max(levels1.length, levels2.length);
    	for(int i = 0; i < length; i++){
    		Integer v1 = i < levels1.length ? Integer.parseInt(levels1[i]) : 0;
    		Integer v2 = i < levels2.length ? Integer.parseInt(levels2[i]) : 0;
    		int compare = v1.compareTo(v2);
    		if(compare != 0)
    			return compare;
    	}
    	return 0;
    }
}
Java [Leetcode 165]Compare Version Numbers的更多相关文章
- 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 比较两个字符串数字的大小  --------- java
		
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 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 ...
 
随机推荐
- 【BZOJ】【1085】【SCOI2005】骑士精神
			
IDA*算法 Orz HZWER A*+迭代加深搜索=IDA* 这题的估价相当于一个可行性剪枝,即如果当前走的步数s+未归位的点数>搜索深度k,则剪枝 /******************** ...
 - map中的erase成员函数用法
			
转载于 http://www.cnblogs.com/graphics/archive/2010/07/05/1771110.html http://hi.baidu.com/sdkinger/it ...
 - win8 任务栏不合并隐藏标题
			
让win8任务栏不合并,并且隐藏标题的办法: 效果如下: 首先让win8不合并任务栏 1.任务栏上点鼠标右键 -- "属性" 2."任务栏按钮"选择" ...
 - What is the difference between Views and Materialized Views in Oracle?
			
aterialized views are disk based and update periodically base upon the query definition. Views are v ...
 - Installing Lua in Mac
			
Lua is distributed in source form. You need to build it before using it. Building Lua should be stra ...
 - POJ1222 高斯消元法解抑或方程
			
第一次学怎么用高斯消元法解抑或方程组,思想其实很简单,方法可以看下面的链接:http://blog.csdn.net/zhuichao001/article/details/5440843 有了这种思 ...
 - cojs 简单的数位DP 题解报告
			
首先这道题真的是个数位DP 我们考虑所有的限制: 首先第六个限制和第二个限制是重复的,保留第二个限制即可 第五个限制在转移中可以判断,不用放在状态里 对于第一个限制,我们可以增加一维表示余数即可 对于 ...
 - ServletRequest中getReader()和getInputStream()只能调用一次的解决办法
			
转载:http://blog.sina.com.cn/s/blog_870cd7b90101fg58.html 最近使用spring mvc做项目,数据格式是json,有一个功能是实现记录请求的参数, ...
 - 编译器的未来——我们还需要C++么?
			
在未来我们还需要纯C++开发模式么? 随着C++11的诞生,C++已经越来越臃肿,从03的时候就觉得C++实在是太复杂了.以一个合格C++程序员的标准来简单的来说3-5年略有小成,5-8年才可以说自己 ...
 - 第二十二章 CLR寄宿和AppDomain
			
1. 概念解析 CLR Hosting(CLR 宿主):初始启动.Net Application时,Windows进程的执行和初始化跟传统的Win32程序是一样的,执行的还是非托管代码,只不过由于PE ...