1. 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.

You may assume the default revision number for each level of a version number to be 0. For example, version number 3.4 has a revision number of 3 and 4 for its first and second level revision number. Its third and fourth level revision number are both 0.

Example 1:

Input: version1 = "0.1", version2 = "1.1"
Output: -1

Example 2:

Input: version1 = "1.0.1", version2 = "1"
Output: 1

Example 3:

Input: version1 = "7.5.2.4", version2 = "7.5.3"
Output: -1

Example 4:

Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation: Ignoring leading zeroes, both “01” and “001" represent the same number “1”

Example 5:

Input: version1 = "1.0", version2 = "1.0.0"
Output: 0
Explanation: The first version number does not have a third level revision number, which means its third level revision number is default to "0"

Note:

  1. Version strings are composed of numeric strings separated by dots . and this numeric strings may have leading zeroes.
  2. Version strings do not start or end with dots, and they will not be two consecutive dots.

解析出每一段版本号进行比较,假设解析出来的数字分别为n1, n2

  • n1 > n2:版本1>版本2
  • n1 < n2:版本1 < 版本2
  • n1 == n2 :比较下一段

在比较到末尾时,长的版本号剩余部分如果全部为0,则版本1 == 版本2,否则长的号对应的版本高

为了方便,在每个版本号后面增加了'#'作为结束符

class Solution {
public:
int compareVersion(string version1, string version2) {
version1.push_back('#');
version2.push_back('#');
int pos1 = 0, pos2 = 0;
int num1 = 0, num2 = 0;
while(version1[pos1] != '#' && version2[pos2] != '#'){
num1 = 0; num2 = 0;
if(version1[pos1] == '.')pos1++;
while(version1[pos1] != '#' && version1[pos1] != '.'){
num1 = num1 * 10 + (version1[pos1++] - '0');
}
if(version2[pos2] == '.')pos2++;
while(version2[pos2] != '#' && version2[pos2] != '.'){
num2 = num2 * 10 + (version2[pos2++] - '0');
}
if(num1 < num2)return -1;
else if(num1 > num2)return 1;
}
if(version1[pos1] == '#' && version2[pos2] == '#'){
return 0;
}else if(version1[pos1] == '#'){
if(allZeros(version2.substr(pos2)))return 0;
else return -1;
}else{
if(allZeros(version1.substr(pos1)))return 0;
else return 1;
}
}
bool allZeros(const string &s){
int test = 0, pos = 0;
while(s[pos] != '#'){
if(s[pos] == '.')pos++;
else test += s[pos++] - '0';
}
if(test == 0)return true;
else return false;
}
};

【刷题-LeetCode】165 Compare Version Numbers的更多相关文章

  1. [LeetCode] 165. Compare Version Numbers 比较版本数

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

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

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

  3. Java for LeetCode 165 Compare Version Numbers

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

  4. Java [Leetcode 165]Compare Version Numbers

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

  5. Leetcode 165 Compare Version Numbers

    题意:比较版本号的大小 有点变态,容易犯错 本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号 class Solution { public: int c ...

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

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

  7. 165. Compare Version Numbers - LeetCode

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

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

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

  9. 【LeetCode】165 - Compare Version Numbers

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

随机推荐

  1. java面向对象类的继承~ 匿名类 ;多态特性;强制类型转换

    类的继承 创建子类语法:     修饰符 class 子类名 extends 父类名{        } 匿名子类语法: 直接实例化,过程中通过匿名类 继承父类,在实例化过程中将子类匿名 <父类 ...

  2. [WPF] 实现 WPF 的 Inner Shadow

    在 WPF 中,我们通常用 DropShadow 做阴影效果,但都是做外阴影.内阴影(Inner Shadow)的话其实也不是不可以,就是有些曲折.这篇文章介绍几种做内引用的做法. 文章涉及到以下概念 ...

  3. 【LeetCode】1150. Check If a Number Is Majority Element in a Sorted Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 二分查找 日期 题目地址:https://lee ...

  4. 【LeetCode】689. Maximum Sum of 3 Non-Overlapping Subarrays 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximum- ...

  5. CS学习资料百度云链接

    CS学习资料百度云链接 [0]Springboot微服务开发天气预报系统视频教程https://pan.baidu.com/s/1joz7flyztCq8oklBlsz8dQ提取密码:cpz7 [1] ...

  6. 生成器执行函数co 源码解读

    本文所选内容均来自[co模块源码] /** * slice() reference. */ var slice = Array.prototype.slice; module.exports = co ...

  7. Java初学者作业——定义一个计算器类, 实现计算器类中加、 减、 乘、 除的运算方法, 每个方法能够接收2个参数。

    返回本章节 返回作业目录 需求说明: 定义一个计算器类, 实现计算器类中加. 减. 乘. 除的运算方法, 每个方法能够接收2个参数. 实现思路: 定义计算器类. 定义计算器类中加.减.乘.除的方法. ...

  8. JavaScript交互式网页设计 • 【第2章 JavaScript函数与事件】

    全部章节   >>>> 本章目录 2.1 JavaScript 自定义函数 2.1.1 函数的定义 2.1.2 函数的调用 2.1.3 函数的参数 2.1.4 函数的返回值 2 ...

  9. MySQL数据库安装Version5.7.25

    1.说明 MySQL数据库大版本5.7的安装没有较大变化, 所以MySQL数据库Version5.7.25安装请参考:MySQL数据库安装Version5.7 上面文章中MySQL安装使用Versio ...

  10. 《Python核心编程第3版中文版》(高清).PDF,免费无需任何解压密码

    链接:https://pan.baidu.com/s/18d3xinNX1oH5q8zpB10ABA 提取码:dx7h