165. Compare Version Numbers (String)
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
注意:版本格式可能会有多个子版本
class Solution {
public:
int compareVersion(string version1, string version2) {
vector<int> arr_v1,arr_v2;
int pos1_pre = , pos2_pre = , pos1=version1.find_first_of('.'),pos2=version2.find_first_of('.');
while(pos1!=-){
arr_v1.push_back(atoi(version1.substr(pos1_pre,pos1).c_str()));
pos1_pre = pos1+;
pos1 = version1.find_first_of('.',pos1_pre);
}
arr_v1.push_back(atoi(version1.substr(pos1_pre).c_str()));
while(pos2!=-){
arr_v2.push_back(atoi(version2.substr(pos2_pre,pos2).c_str()));
pos2_pre = pos2+;
pos2 = version2.find_first_of('.',pos2_pre);
}
arr_v2.push_back(atoi(version2.substr(pos2_pre).c_str()));
int size1 = arr_v1.size();
int size2 = arr_v2.size();
int size = min(size1,size2);
for(int i = ; i < size; i++){
if(arr_v1[i] > arr_v2[i]) return ;
else if(arr_v1[i] < arr_v2[i]) return -;
}
if(size == size1){
for(int i = size; i < size2; i++){
if(arr_v2[i]!=) return -;
}
return ;
}
else{
for(int i = size; i < size1; i++){
if(arr_v1[i]!=) return ;
}
return ;
}
}
};
165. Compare Version Numbers (String)的更多相关文章
- 165. Compare Version Numbers - LeetCode
Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...
- 【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 ...
- ✡ 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 ...
- 165. Compare Version Numbers
题目: Compare two version numbers version1 and version2.If version1 > version2 return 1, if version ...
- 【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
随机推荐
- ABAP-2-会计凭证批量数据导入本地ACCESS
ABAP-1-会计凭证批量数据导入本地ACCESS 上一版本出现问题: A.若TXT数据条目超过800万(大概1.3G),则将TXT导入ACCESS过程不成功,ACCESS数据表为空.(Access单 ...
- linux之Ubuntu下Django+uWSGI+nginx部署
http://www.chenxm.cc/post/275.html?segmentfault
- sourcetree 添加私钥
参考网址: https://www.jianshu.com/p/2a4a39e3704f
- 尚硅谷redis学习4-数据类型
redis的数据类型包括String,Hash(类似于JAVA里的map),List,Set,Zset(sorted Set) String(字符串) string是redis最基本的类型,你可以理解 ...
- it工程师常用英文自我介绍常用用语
Good morning ! It is really my honor to have this opportunity for an interview, I hope i can make ...
- 16.监听器-基础.md
目录 概念 监听器功能 监听创建或销毁 监听属性变化 session相关监听 概念 实现特定接口的普通java类 *监听特定对象的创建.销毁或属性变化: 对象自己创建自己用:无需监听 对象别人创建自己 ...
- Java复习 之流
在Java程序中 对于数据的输入/输出操作以“流”方式进行:提供了各种各样的流类,用以获取各种不同的种类的数据,程序中通过标准的方法输入或输出数据 Inputstream 例子1: 但是中文会乱码 应 ...
- windows下一分钟配置ngnix实现HLS m3u8点播
1. 下载 nginx-1.5.10 for windows 2. 修改配置文件nginx-1.5.10\conf\nginx.conf,增加以下行到最后一个"}"的前一行: lo ...
- 吴裕雄 python深度学习与实践(5)
import numpy as np data = np.mat([[1,200,105,3,False], [2,165,80,2,False], [3,184.5,120,2,False], [4 ...
- Python之路 - 网络编程之粘包
Python之路 - 网络编程之粘包 粘包