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 ...
随机推荐
- Redis中7种集合类型应用场景
StringsStrings 数据结构是简单的key-value类型,value其实不仅是String,也可以是数字.使用Strings类型,你可以完全实现目前 Memcached 的功能,并且效率更 ...
- XmlElement 类
构造函数 名称 说明 XmlElement(String, String, String, XmlDocument) 此 API 支持 产品 基础结构,不能在代码中直接使用. 初始化 XmlEle ...
- 李洪强-C语言9-C语言的数据,变量和常量
一.数据 图片文字等都是数据,在计算机中以0和1存储. (一)分类 数据分为静态数据和动态数据. ①. 静态数据:一些永久性的的数据,一般存储在硬盘中,只要硬盘没坏数据都是存在的.一般以文件的形式存储 ...
- Asp.Net 常用代码-备用
1.DropDownList 复制 //DropDownList DropDownList2.Items.Clear(); foreach (ListItem li in DropDownList1. ...
- 将服务器上的某些特定日志统一保存至XPS文件
<APACHost.CSV> Hostname,IP,OSType srv1,10.103.22.22,Win2003 srv2,10.103.22.37,Win2008 Import ...
- POJ 3041 匈牙利算法模板题
一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分 ...
- 推荐给开发者的20个优秀PHP框架
推荐给开发者的20个优秀PHP框架 来源:developerslane 时间:2015-01-13 19:48:06 阅读数:111916 分享到:14 [导读] PHP是非常受欢迎并且很有影 ...
- BCP 命令
bcp 实用工具 我们可以通过如下BCP命令(注意不能有回车)将其导出为XML文件,并保存: BCP "SELECT TOP 30 [bom_no],[LEVEL] FROM [sqladm ...
- communication between threads 线程间通信 Programming Concurrent Activities 程序设计中的并发活动 Ada task 任务 Java thread 线程
Computer Science An Overview _J. Glenn Brookshear _11th Edition activation 激活 parallel processing 并行 ...
- 不绑架输入--document.getElementById("linkage_"+id_type+"_echo").value="";--联动
<script> function w_linkage(id_type) { var selected = $("#linkage_"+id_type+"_t ...