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.
Example 1:
Input:version1= "0.1",version2= "1.1"
Output: -1
Example 2:
Input:version1= "1.0.1",version2= "1"
Output: 1
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为要用stack,结果是个水题
[英文数据结构或算法,为什么不用别的数据结构或算法]:
一般就split就行了。.是特殊符号,要加双反斜杠.split("\\.")
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
.是特殊符号,要加双反斜杠.split("\\.")
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int compareVersion(String version1, String version2) {
//corner case
if (version1 == null && version2 == null) return 0;
//initialization: split
String[] words1 = version1.split("\\.");
String[] words2 = version2.split("\\.");
for (int i = 0; i < Math.max(words1.length,words2.length); i++) {
//get 2 nums
int num1 = (i < words1.length) ? Integer.valueOf(words1[i]) : 0;
int num2 = (i < words2.length) ? Integer.valueOf(words2[i]) : 0;
//compare and return
if (num1 > num2) {
return 1;
}else if (num1 < num2) {
return -1;
}
}
return 0;
}
}
165. Compare Version Numbers比较版本号的大小的更多相关文章
- 165 Compare Version Numbers 比较版本号
比较两个版本号 version1 和 version2.如果 version1 大于 version2 返回 1,如果 version1 小于 version2 返回 -1, 除此以外 返回 0.您可 ...
- 【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 ...
- 【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 165. Compare Version Numbers
题目: Compare two version numbers version1 and version2.If version1 > version2 return 1, if version ...
- [LeetCode] 165. Compare Version Numbers 比较版本数
Compare two version numbers version1 and version1.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 ...
随机推荐
- MariaDB管理系统
MariaDB管理系统 [root@c4kaichen@163 ~]# yum install mariadb[root@c4kaichen@163 ~]# yum install -y mariad ...
- 1114 Family Property (25 分)
1114 Family Property (25 分) This time, you are supposed to help us collect the data for family-owned ...
- 1073 Scientific Notation (20 分)
1073 Scientific Notation (20 分) Scientific notation is the way that scientists easily handle very la ...
- ubuntu 14.04 lamp 安装与配置
一.安装apache 1.打开终端:Ctrl+Alt+T sudo apt-get update 2.通过apt-get方式安装Apache: sudo apt-get install apache2 ...
- xgCalendar在ASP.NET中的使用
1.将wdCalendar文件夹考入项目中 2.在页面中添加引用,见3中head标签中定义 3.配置xgCalendar,两段代码放在一起就是完整的页面 body> <div> &l ...
- 3D Render
记录最近遇到的问题: 1:崩溃问题 由于高频率获取DC异常导致. void D3D11Texture2D::Copy2Window(void* srcdc, uint32_t left, uint32 ...
- Spark Streaming 例子
NetworkWordCount.scala /* * Licensed to the Apache Software Foundation (ASF) under one or more * con ...
- 序列化 ,hashlib ,configparser ,logging ,collections模块
# 实例化 归一化 初始化 序列化 # 列表 元组 字符串# 字符串# .......得到一个字符串的结果 过程就叫序列化# 字典 / 列表 / 数字 /对象 -序列化->字符串# 为什么要序列 ...
- jmeter-noguimodel
jmeter -Dthreads= -n -t ~/Desktop/image-controller.jmx -l myimage/out -e -o myimage/log -j myimage/r ...
- robot framework取出列表子元素
取出嵌套列表变量的子元素 ${list}型列表: ${list} = [["A1", "first"], ["A2", "seco ...