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 ...
随机推荐
- Spring声明式事务配置管理方法
环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add ...
- Java实现FTP文件上传与下载
实现FTP文件上传与下载可以通过以下两种种方式实现(不知道还有没有其他方式),分别为:1.通过JDK自带的API实现:2.通过Apache提供的API是实现. 第一种方式 package com.cl ...
- .NET Framework 4.5 的五大特性
介绍 从.NET4.5发布到现在已经有一年多了.但问题是针对最近微软发布的版本信息中,大部分的.NET开发人员所讨论交流的只是其中的一两个特性.其他的特性仅仅停留在MSDN中或者沦为简介文档.例如:现 ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 如何使用Xcode分析调试在真机运行的UE4 IOS版游戏
写本文的是因为UE4 官方文档虽然也有,但主要讲的是是用UE4Editor把游戏打成一个IPA包的形式发布的方法 而对于想通过Xcode分析UE4的渲染流程来学习或优化的朋友,那官方文档的资料还是不够 ...
- 总结一下这几天学习django的心得
总结一下这几天学习django的心得 http://www.tuicool.com/articles/jMVB3e 时间 2014-01-12 11:40:11 CSDN博客 原文 http:// ...
- 从 github 执行 git clone 一个大的项目时提示 error: RPC failed
目前克隆一个比较大的项目,出现RPC failed的错误 Cloning into 'bigfiles'... remote: Counting objects: 190, done. remote: ...
- phpspec安装配置
安装 composer require phpspec/phpspec -dev 运行 bin/phpspec 在laravel中 vendor/bin/phpspec 配置phpspec.ym ...
- single-chip microcomputer Microcontroller 单片机 单片微型计算机 微控制器
https://zh.wikipedia.org/wiki/单片机 单片机,全称单片微型计算机(英语:single-chip microcomputer),又称微控制器(microcontroller ...
- instruction-set architecture Processor Architecture
Computer Systems A Programmer's Perspective Second Edition We have seen that a processor must execut ...