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

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Hide Tags

String

 

  这题是太多细节的操作,字符串string 的操作其实很简单,主要判断0的情况,需要考虑1.1.0 与1.1 是相等的。代码写的有点冗余,如果再第一个while 中修改可以改短。
 
#include <iostream>
#include <string>
using namespace std; class Solution {
public:
int compareVersion(string version1, string version2) {
if (version1.length()<||version2.length()<) return ;
int beg1 = ,beg2 = ;
int end1 = ;
int end2 = ; while(end1!=string::npos&&end2!=string::npos){
end1 = version1.find_first_of('.',beg1);
end2 = version2.find_first_of('.',beg2);
int int1 = hFun(version1.substr(beg1,end1-beg1));
int int2 = hFun(version2.substr(beg2,end2-beg2));
if(int1 > int2) return ;
if(int1 < int2) return -;
beg1 = end1 +;
beg2 = end2 +;
}
if(end1==string::npos){
while(end2!=string::npos){
end2 = version2.find_first_of('.',beg2);
int int2 = hFun(version2.substr(beg2,end2-beg2));
if(int2 >) return -;
beg2 = end2+;
}
}
if(end2==string::npos){
while(end1!=string::npos){
end1 = version1.find_first_of('.',beg1);
int int1 = hFun(version1.substr(beg1,end1-beg1));
if(int1 >) return ;
beg1 = end1+;
}
}
return ;
}
int hFun(string s)
{
int n = ;
for(int i=;i<s.length();i++){
n*=;
n+=s[i]-'';
}
return n;
}
}; int main()
{
string version1 = "1.1";
string version2 = "";
Solution sol;
cout<<sol.compareVersion(version1,version2)<<endl;
return ;
}
 
 
 
  

[LeetCode] Compare Version Numbers 字符串操作的更多相关文章

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

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

  2. LeetCode Compare Version Numbers

    原题链接在这里:https://leetcode.com/problems/compare-version-numbers/ 用string.split()方法把原有string 从小数点拆成 str ...

  3. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

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

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

  5. 165. Compare Version Numbers - LeetCode

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

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

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

  7. 2016.5.21——Compare Version Numbers

    Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A'   ) 2.srt.at(),substr ...

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

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

  9. LeetCode OJ:Compare Version Numbers(比较版本字符串)

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

随机推荐

  1. 项目17-超详细“零”基础kafka入门篇

    分类: Linux服务篇,Linux架构篇   1.认识kafka 1.1 kafka简介 Kafka 是一个分布式流媒体平台 kafka官网:http://kafka.apache.org/ (1) ...

  2. yii2 基本的增删改查

    一:添加方法 1.1 使用成员属性的方式 save $user_name = $_POST['user_name']; $password = $_POST['password']; //实例化 $u ...

  3. python 3 在工作中的应用

    Python 3在工作中的使用 安装配置Python 3 在notepad++中配置Python 3 使用sql server数据库 操作Excel 发送email python 3 使用日志   安 ...

  4. python正则表达式入门篇

    文章来源于:https://www.cnblogs.com/chuxiuhong/p/5885073.html Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. ...

  5. UML类图关系模式(C++代码说明)

    在UML类图中的关系模式主要有以下几种: 泛化(Generalization),  实现(Realization), 关联(Association), 聚合(Aggregation), 依赖(Depe ...

  6. 【luminate primordial】苏州之行

    测试了reader 07版 更主要的是第一次坐了高铁 还不错 路上看到下雨的时候都是水顺着玻璃平着流 好厉害的样子 6个人去的6个人回 今儿开会 老板不太满意 小随意 对我来说,收获感觉还是不小的,使 ...

  7. 【Akroma, Angel of Fury】完成svn环境搭建

    昨天的那篇博文恰恰是实验室所干的事儿 但是那是一种很投机取巧的方式完成的多project管理方式 来看看我建立环境的方法 首先,找一个比较闲的公用服务器(为什么不用自己的?有公共资源不用,你傻啊?), ...

  8. [USACO]Bovine Genomics

    Description 给定两个字符串集合A,B,均包含N个字符串,长度均为M,求一个最短的区间[l,r],使得不存在字符串\(a\in A,b\in B,\)且\(a[l,r]=b[l,r]\) , ...

  9. git上传自己的代码

    感谢这个哥们的博客,不过里面有些错误. http://www.cnblogs.com/ruofengzhishang/p/3842587.html 下面是我自己的实践成功的: 这篇文章写得是windo ...

  10. 2.使用vue ui命令快速构建应用

    直接在web端新建应用 C:\Users\Hugo> vue ui