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)的更多相关文章

  1. 165. Compare Version Numbers - LeetCode

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

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

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

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

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

  4. ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java

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

  5. Java for LeetCode 165 Compare Version Numbers

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

  6. 【LeetCode】165 - Compare Version Numbers

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

  7. Java [Leetcode 165]Compare Version Numbers

    题目描述: Compare two version numbers version1 and version2.If version1 > version2 return 1, if versi ...

  8. 165. Compare Version Numbers

    题目: Compare two version numbers version1 and version2.If version1 > version2 return 1, if version ...

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

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

随机推荐

  1. js处理数据库时间格式/Date(1332919782070)/

    js处理数据库时间格式 数据库返回时间格式:/Date(1332919782070)/ 方法: function ChangeDateFormat(val) { if (val != null) { ...

  2. 选择、操作web元素-3

    11月5日 Selenium 作业 3 登录 51job , http://www.51job.com 输入搜索关键词 "python", 地区选择 "杭州"( ...

  3. java通过过滤器 设置跨域允许

    前人种树:https://www.cnblogs.com/CBDoctor/p/4235082.htmlhttps://blog.csdn.net/yuting0787/article/details ...

  4. linux查询硬件信息

    硬件信息查询 sudo dmidecode -t baseboard

  5. C++复习:继承与派生

    1继承概念 面向对象程序设计有4个主要特点:抽象.封装.继承和多态性.说了类和对象,了解了面向对象程序设计的两个重要特征一数据抽象与封装,已经能够设计出基于对象的程序,这是面向对象程序设计的基础. 要 ...

  6. bcrelay广播包转发器

    https://www.mankier.com/8/bcrelay PPTP原是基于PPP的三层通信协议,加入bcrelay后可以将二层的广播包转发到PPTP的client端 在openwrt中实现的 ...

  7. t959 unknown device 解决办法

    换机器没用 换数据线没用 最后装了Kies3,好了! -------- 更新 跟数据线也有关系 换一条三星自带的试试

  8. [Linux]Linux下动态安装PHP扩展的一般方法(图)

    ---------------------------------------------------------------------------------------------------- ...

  9. Asp.Net WebApi 学习记录(一)

    刚创建的 Asp.Net Web Api 项目,在进行简单的测试时发现返回的 JSON 数据很丑陋.与平时我们使用的 JSON.NET 序列化出来的字符串不一样.通过下面的设置就可以了: // 清除所 ...

  10. [转载]FMS Dev Guide学习笔记(验证用户)

    一.开发交互式的媒体应用程序 1.使用外部资源验证用户 对于有限数量的客户,请求用户名密码,然后通过外部资源(像数据库.LDAP服务或其它访问授权服务)验证它们,是可行的.     a.SWF在请求连 ...