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
链接: http://leetcode.com/problems/compare-version-numbers/
题解:
比较版本大小。这道题目可以把输入两个string split一下,注意因为"."是特殊regex,所以要用"\\."。Split完毕以后比较array每个对应元素的大小就可以了。
Time Complexity - O(n), Splace Complexity - O(n)。
public class Solution {
public int compareVersion(String version1, String version2) {
if(version1 == null || version2 == null)
return 0;
String[] version1Arr = version1.split("\\.");
String[] version2Arr = version2.split("\\.");
int i = 0, j = 0; while(i < version1Arr.length || j < version2Arr.length) {
int ver1 = i < version1Arr.length ? Integer.parseInt(version1Arr[i]) : 0;
int ver2 = j < version2Arr.length ? Integer.parseInt(version2Arr[i]) : 0;
if(ver1 < ver2)
return -1;
else if(ver1 > ver2)
return 1;
i++;
j++;
} return 0;
}
}
假如不想用split,那么可以节约space complexity。这里我们不用parseInt,要使用手动计算每个level version的数字,然后加以比较。
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public int compareVersion(String version1, String version2) {
if(version1 == null || version2 == null)
return 0; int ver1 = 0, ver2 = 0, i = 0, j = 0;
while(i < version1.length() || j < version2.length()){
while(i < version1.length() && version1.charAt(i) != '.'){
ver1 = ver1 * 10 + version1.charAt(i) - '0';
i++;
} while(j < version2.length() && version2.charAt(j) != '.' ){
ver2 = ver2 * 10 + version2.charAt(j) - '0';
j++;
} if(ver1 < ver2)
return -1;
else if(ver1 > ver2)
return 1;
else{
ver1 = 0;
ver2 = 0;
i++;
j++;
}
}
return 0;
}
}
题外话: 双11特意Work from home在家抢宝贝,不过总觉得没啥可买的,浏览来浏览去,到最后也只花了600多块,买了几本书和一个行车记录仪...不够阔气啊,于是订了Jewel Bako明天去找找感觉。
二刷:
方法跟一刷一样
Java:
Time Complexity - O(n), Splace Complexity - O(n)。
public class Solution {
public int compareVersion(String version1, String version2) {
if (version1 == null || version2 == null) {
return 0;
}
String[] ver1Arr = version1.split("\\.");
String[] ver2Arr = version2.split("\\.");
int len1 = ver1Arr.length, len2 = ver2Arr.length, lo1 = 0, lo2 = 0;
while (lo1 < len1 || lo2 < len2) {
int ver1 = lo1 < len1 ? Integer.parseInt(ver1Arr[lo1]) : 0;
int ver2 = lo2 < len2 ? Integer.parseInt(ver2Arr[lo2]) : 0;
if (ver1 < ver2) {
return -1;
} else if (ver1 > ver2) {
return 1;
} else {
lo1++;
lo2++;
}
}
return 0;
}
}
不用split
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public int compareVersion(String version1, String version2) {
if (version1 == null || version2 == null) {
return 0;
}
int len1 = version1.length(), len2 = version2.length(), lo1 = 0, lo2 = 0, ver1 = 0, ver2 = 0;
while (lo1 < len1 || lo2 < len2) {
while (lo1 < len1 && version1.charAt(lo1) != '.') {
ver1 = 10 * ver1 + version1.charAt(lo1) - '0';
lo1++;
}
while (lo2 < len2 && version2.charAt(lo2) != '.') {
ver2 = 10 * ver2 + version2.charAt(lo2) - '0';
lo2++;
}
if (ver1 < ver2) {
return -1;
} else if (ver1 > ver2) {
return 1;
} else {
lo1++;
lo2++;
ver1 = 0;
ver2 = 0;
}
}
return 0;
}
}
三刷:
Java:
public class Solution {
public int compareVersion(String version1, String version2) {
if (version1 == null || version2 == null) return 0;
String[] v1s = version1.split("\\.");
String[] v2s = version2.split("\\.");
int i = 0, j = 0, res = 0;
while (i < v1s.length || j < v2s.length) {
int ver1 = i < v1s.length ? Integer.valueOf(v1s[i++]) : 0;
int ver2 = j < v2s.length ? Integer.valueOf(v2s[j++]) : 0;
if (ver1 < ver2) return -1;
else if (ver1 > ver2) return 1;
}
return 0;
}
}
Reference:
http://www.fromdev.com/2009/10/playing-with-java-string-split-basics.html
165. Compare Version Numbers的更多相关文章
- 【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 比较两个字符串数字的大小 --------- java
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- 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
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Java [Leetcode 165]Compare Version Numbers
题目描述: Compare two version numbers version1 and version2.If version1 > version2 return 1, if versi ...
- 【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 165. Compare Version Numbers (String)
Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 & ...
随机推荐
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
- SQL Server 负载均衡集群方案之Moebius
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 架构原理(Architecture) 测试环境(Environment) 安装Moebius( ...
- Drbd 安装配置
一.Drbd介绍 Distributed Replicated Block Device(DRBD)是基于块设备在不同的高可用服务器之间同步和镜像数据的软件,通过它可以实现在网络中两台服务器这间基于块 ...
- 源代码jar包中中文注释乱码
目前公司开发的多个组件有打包源代码并发布到nexus,但是很多同事通过maven使用组件时,直接通过eclipse浏览源代码时,发现中文注释为乱码的问题.其实这个eclipse默认编码造成的问题.可以 ...
- 【mysql】【分组】后取每组的top2
DROP TABLE IF EXISTS `tb1`; CREATE TABLE `tb1` ( `id` ) NOT NULL AUTO_INCREMENT, `a` ) DEFAULT NULL, ...
- css3中的BFC,IFC,GFC和FFC(转载)
作者原文网址:http://www.cnblogs.com/dingyufenglian/p/4845477.html What‘s FC? 一定不是KFC,FC的全称是:Formatting C ...
- 一种c#深拷贝方式完胜java深拷贝(实现上的对比)
楼主是一名asp.net攻城狮,最近经常跑java组客串帮忙开发,所以最近对java的一些基础知识特别上心.却遇到需要将一个对象深拷贝出来做其他事情,而原对象保持原有状态的情况.(实在是不想自己new ...
- IO流02_文件过滤器
[简述] 在File类的list方法中可以接收一个FilenameFilter参数,通过该参数可以列出只满足要求的文件. FilenameFilter接口里包含了一个accept(File dir, ...
- C++实用数据结构:二叉索引树
看下面这个问题(动态连续和查询): 有一个数组A(长度为n),要求进行两种操作: add(i,x):让Ai增大x: query(a,b):询问Aa+Aa+1+...+Ab的和: 若进行模拟,则每次qu ...
- Demo02_对结构体进行文件读写_张仕传_作业_
#include <iostream> using namespace std; #define StructArrarySize 5 // 老师数量 #define StudentNum ...