【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
来源: https://leetcode.com/problems/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.
Here is an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
(二)解题
题目大意:给定两个版本号码,比较他们的大小。
解题思路:版本号一般为1.1.2,中间以点(.)隔开,要判断版本号的大小,需要计算每个以点隔开的数的大小。
需要注意一些特殊情况:
(1) 00001.1.1这种前面有0干扰的
(2) 1.0.0.0.0和1是相等的
详细思路见如下代码和注释:
class Solution {
public:
int compareVersion(string version1, string version2) {
int len1= version1.length();
int len2= version2.length();
int i = 0;
int j = 0;
while(i<len1&&j<len2)//首先计算两个版本号相同位置的数的大小
{
int num1 =countnum(version1,i);
int num2 =countnum(version2,j);
i++;j++;
if(num1==num2) continue;
else return num1>num2?1:-1;
}
if(i>=len1&&j<len2)//如果version1比较完了,则判断version2后面是否全为0
{
while(j<len2)
{
int num2 =countnum(version2,j);
j++;
if(num2==0) continue;//等于0就继续往后
else return -1;//否则就返回version2大
}
return 0;//全为0就返回相等
}
if(i<len1&&j>=len2)//同上,如果version2比较完了,则判断version1后面是否全为0
{
while(i<len1)
{
int num1 =countnum(version1,i);
i++;
if(num1==0) continue;//等于0就继续往后判断
else return 1;//否则就返回version1大
}
return 0;//全为0就返回相等
}
return 0;//上述比较都相等就返回相等
}
int countnum(string s , int& i)//用来计算以点隔开的数字
{
int len = s.length();
int num = 0;
while(i<len&&s[i]!='.')//碰到结束或者'.'
{
num = num*10+(s[i]-'0');
i++;
}
return num;
}
};
【一天一道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
Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...
- 【LeetCode】165 - Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- shell基本命令
linux基本命令和shell基本命令,好多人傻傻分不清. linux基本命令积累如下: pwd:显示当前工作目录 cd:改变当前目录 ls:显示当前目录中所有目录文件和文本文件 ls -F:显示当前 ...
- Java 反射实现实体转Map时,父类元素丢失
public class BeanToMap { public static Map<String, Object> ConvertObjToMap(Object obj) { Map&l ...
- 数组查找算法的C语言 实现-----线性查找和二分查找
线性查找 Linear Search 用户输入学生学号的成绩 二分查找 Binary Search 要求数据表是已经排好序的 程序存在小的瑕疵
- 解决Mysql数据库拒绝远程连接和忘记密码的问题
解决数据库忘记密码的问题 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 编辑m ...
- zookeeper工作机制
Zookeeper Zookeeper概念简介: Zookeeper是为用户的分布式应用程序提供协调服务的 zookeeper是为别的分布式程序服务的 Zookeeper本身就是一个分布式程序(只要有 ...
- c++银行家算法
#include <iostream> #include<string> #define False 0 #define True 1 using namespace std; ...
- centos6 安装redis-4.0.9
从redis官网下载Linux redis4.0.9版本,我下载的redis-4.0.9.tar.gz(目前最新稳定版),下载到/usr/local/src目录,如果没有就mkdir创建一个. 下载链 ...
- Git 常用命令速查表(图文+表格)
一. Git 常用命令速查 git branch 查看本地所有分支git status 查看当前状态 git commit 提交 git branch -a 查看所有的分支git branch -r ...
- 安卓高级9 用原生intent分享
大家都用过安卓app时发现有个分享按钮如下: 所以今天特此分享用用原生完成: package qianfeng.com.simplesharedemo; import android.content. ...
- 20160214.CCPP体系详解(0024天)
程序片段(01):CGI.c 内容概要:CGI-cloud #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int main01(vo ...