LeetCode Compare Version Numbers
原题链接在这里:https://leetcode.com/problems/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
题解:
用string.split()方法把原有string 从小数点拆成 string 数组,但这里要注意 . 和 * 是不能直接用split(".") 或者split("*")拆开的,因为 . 可以代表任意char, * 可以代表任意字符串。所以要加 \\. 来避免individual special character.
拆开后用Interger.valueOf()转换成数字直接比较就好。
拆完后两个数组长度可能不同, "1" 和 "1.1", 所以while 循环的条件是i < ver1.length 或者 i<ver2.length.
Time Complexity: O(Math.max(version1.length, version2.length)), 因为用了split.
Space: O(version1.length + version2.length), 建立了array.
AC Java:
 class Solution {
     public int compareVersion(String version1, String version2) {
         if(version1 == null || version2 == null){
             throw new IllegalArgumentException("Invalid input string.");
         }
         String [] v1 = version1.split("\\.");
         String [] v2 = version2.split("\\.");
         int i = 0;
         while(i<v1.length || i<v2.length){
             int a = i<v1.length ? Integer.valueOf(v1[i]) : 0;
             int b = i<v2.length ? Integer.valueOf(v2[i]) : 0;
             if(a < b){
                 return -1;
             }else if(a > b){
                 return 1;
             }
             i++;
         }
         return 0;
     }
 }
LeetCode Compare Version Numbers的更多相关文章
- [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 version1 &l ...
 - 【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 ...
 - 2016.5.21——Compare Version Numbers
		
Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A' ) 2.srt.at(),substr ...
 - 【一天一道LeetCode】#165. Compare Version Numbers
		
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
 - [LeetCode] 165. Compare Version Numbers 比较版本数
		
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
 
随机推荐
- spark Mllib SVM实例
			
Mllib SVM实例 1.数据 数据格式为:标签, 特征1 特征2 特征3…… 0 128:51 129:159 130:253 131:159 132:50 155:48 156:238 157: ...
 - 移动web app开发小贴士 收藏有用
			
1 创建主屏幕图标 (Creating a home screen icon ,for ios) 1 2 3 4 5 6 //57*57 <link rel="apple-touc ...
 - 零宽度正预测先行断言是什么呢,看msdn上的官方解释定义
			
最近为了对html文件进行源码处理,需要进行正则查找并替换.于是借着这个机会把正则系统地学一下,虽然以前也用过正则,但每次都是临时学一下混过关的.在学习的过程中还是遇到不少问题的,特别是零宽断言(这里 ...
 - 【iCore2 双核心板视频教程一】iM_LAN 100M 以太网模块UDP例程(包含视频教程)
			
============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...
 - The resource could not be loaded because the App Transport Security policy requires the use of a secure connection
			
xmpp 项目中遇到的问题,用苹果的通信API 写一个PUT 方法,向服务器上传一张图片.遇到如题问题. Plist 文件没有NSAppTransportSecurity属性 Dic,添加该属性,再添 ...
 - PHP CURL 多线程 GET/POST 类
			
PHP CURL 多线程 GET/POST 类 2015-01-01 分类:技术文章 阅读(623) 评论(0) 如果有需要更正或更高效的建议,欢迎在OSchina分享~\(≧▽≦)/~ http:/ ...
 - Yii源码阅读笔记(五)
			
Object 是一个基础类,实现了属性的功能,其基本内容如下: namespace yii\base; use Yii; /** * Object is the base class that imp ...
 - Happy
			
1.delighted 2.over the moon Alex is over the moon with promotion. 3.really pleased ...
 - P1018 乘积最大
			
开始定义状态f[i][j][k]为[i,j)区间插入k个括号,使用记忆化搜索,但是成功爆栈,得到4个mle #include <bits/stdc++.h> using namespace ...
 - ubuntu 制作deb 包
			
ubuntu下打包制作deb安装包 http://www.th7.cn/system/lin/201406/61012.shtml 2014-06-22 20:16:45CSDN-yangbing ...