【刷题-LeetCode】165 Compare Version Numbers
- 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:
- Version strings are composed of numeric strings separated by dots
.and this numeric strings may have leading zeroes. - 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的更多相关文章
- [LeetCode] 165. Compare Version Numbers 比较版本数
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- ✡ 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 ...
- 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
题意:比较版本号的大小 有点变态,容易犯错 本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号 class Solution { public: int c ...
- 【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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【LeetCode】165 - Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- Raft论文概述
介绍 Raft是一种为了管理复制日志的一致性算法.为了提升可理解性,Raft 将一致性算法分解成了几个关键模块,例如领导人选举.日志复制和安全性.同时它通过实施一个更强的一致性来减少需要考虑的状态的数 ...
- IDEA 无法显示 Run Dashboard 的解决方法
前言 最近开始接触微服务的项目,项目很多有时候本地调测需要启动多个项目,看到同事都是使用dashboard管理项目,服务和端口排列的整整齐齐.但是我dashboard里面啥都没有,一顿百度最后解决问题 ...
- 【linux项目】lichee nano linux烧写
目录 前言 参考: 安装交叉编译链 搭建 SPI FLASH 烧录环境 让芯片进入烧写模式 sunxi 烧写命令 u-boot 裁剪 拉取 u-boot 源码 配置 u-boot 检查 flash 驱 ...
- 【LeetCode】259. 3Sum Smaller 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 双指针 日期 题目地址:https://le ...
- select......for update会锁表还是锁行
select查询语句是不会加锁的,但是select .......for update除了有查询的作用外,还会加锁呢,而且它是悲观锁. 那么它加的是行锁还是表锁,这就要看是不是用了索引/主键. 没用索 ...
- delete、truncate、drop
DELETE DELETE属于数据库DML操作语言,只删除数据不删除表的结构,会走事务,执行时会触发trigger:每次从表中删除一行,并且同时将该行的的删除操作记录在redo和undo表空间中以便进 ...
- 使用zTree插件实现可拖拽的树
在目前接触到的树插件中,我觉得zTree比较简单,也容易上手.有一次业务需求是将某对象分组树上的对象可以随意拖拽,相当于改变了对象的分组,因此我用到了zTree,对其进行了一些列学习. ...
- Crypto CTF 2019 writeup
Crypto CTF 2019 writeup roXen 题目 roXen Relationship with a cryptographer! The Girlfriend: All you ev ...
- 山寨F407板子如何烧录正点原子例程、Keil下载提示Invalid ROM Table
山寨F407板子如何烧录正点原子例程 手头这块块信赢达的STM32F407板子,是模仿正点原子探索者STM32F407板子的,外设和接口,几乎是一模一样. 探索者板子用的是STM32F407ZET6, ...
- Linux常用命令,新手可以看看
最近在温习了一些linux的命令,这里总结一下,博主使用的系统是Ubuntu,版本如下: 由于博主是做开发的,所有linux只会一些常用的命令,跟那些专业linux大牛当前没的比,为什么Ubuntu而 ...