原题链接在这里: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的更多相关文章

  1. [LeetCode] Compare Version Numbers 版本比较

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

  2. [LeetCode] Compare Version Numbers 字符串操作

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  3. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

  4. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. 【刷题-LeetCode】165 Compare Version Numbers

    Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...

  6. 165. Compare Version Numbers - LeetCode

    Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...

  7. 2016.5.21——Compare Version Numbers

    Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A'   ) 2.srt.at(),substr ...

  8. 【一天一道LeetCode】#165. Compare Version Numbers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  9. [LeetCode] 165. Compare Version Numbers 比较版本数

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

随机推荐

  1. C# - Lee 公共类库

    我的公共类库 using System; using System.IO; using System.Net; using System.Security.Cryptography; using Sy ...

  2. [转]用Linq取CheckBoxList選取項目的值

    本文转自:http://www.dotblogs.com.tw/hatelove/archive/2011/11/17/linq-checkboxlist-items-selected-values. ...

  3. LeetCode | Unique Paths【摘】

    A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The ...

  4. Transform-style和Perspective属性

    transform-style属性 transform-style属性是3D空间一个重要属性,指定嵌套元素如何在3D空间中呈现.他主要有两个属性值:flat和preserve-3d. transfor ...

  5. JQuery文件上传插件uploadify在MVC中Session丢失的解决方案

    <script type="text/javascript"> var auth = "@(Request.Cookies[FormsAuthenticati ...

  6. 《Ant权威指南》笔记(一)

    Ant的由来(序) James Duncan Davidson当年用纯Java开发Tomcat的时候,不仅想让它跨平台运行,还想要在不同的操作系统上都能够进行开发和构建.这种较大的项目的编译构建过程是 ...

  7. twitter storm源码走读之2 -- tuple消息发送场景分析

    欢迎转载,转载请注明出处源自徽沪一郎.本文尝试分析tuple发送时的具体细节,本博的另一篇文章<bolt消息传递路径之源码解读>主要从消息接收方面来阐述问题,两篇文章互为补充. worke ...

  8. 免费手机号码归属地API查询接口和PHP使用实例分享

    免费手机号码归属地API查询接口和PHP使用实例分享 最近在做全国性的行业分类信息网站,需要用到手机号归属地显示功能,于是就穿梭于各大权威站点之间偷来了API的接口地址. 分享出来,大家可以用到就拿去 ...

  9. 不再以讹传讹,GET和POST的真正区别

    不再以讹传讹,GET和POST的真正区别 网上的多数答案都是错的 在 2012年05月03日 那天写的     已经有 19940 次阅读了 感谢 参考或原文 www.cnblogs.com   服务 ...

  10. ecshop缓存清理-限制或禁用ECShop缓存

    ecshop缓存清理-限制或禁用ECShop缓存   ECSHOP的缓存存放在templates/caches/文章夹下,时间长了这个文件夹就会非常庞大,拖慢网站速度.还有很多情况我们不需要他的缓存. ...