[LeetCode] 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
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
#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 字符串操作的更多相关文章
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- LeetCode Compare Version Numbers
原题链接在这里:https://leetcode.com/problems/compare-version-numbers/ 用string.split()方法把原有string 从小数点拆成 str ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
- 【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 ...
- 2016.5.21——Compare Version Numbers
Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A' ) 2.srt.at(),substr ...
- ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- LeetCode OJ:Compare Version Numbers(比较版本字符串)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- phpspider案例
phpspider案例 <?php require './autoload.php'; use phpspider\core\phpspider; /* Do NOT delete this c ...
- Net core 轮子
.net core 使用的人渐渐多了起来,轮子也渐渐多了起来,为了避免重复造轮子,以下列举了一些造好的轮子 1. IP 请求频率限制 git: https://github.com/stefanpro ...
- Flask学习笔记:数据库ORM操作MySQL+pymysql/mysql-python+SQLAlchemy/Flask-SQLAlchemy
Python中使用sqlalchemy插件可以实现ORM(Object Relationship Mapping,模型关系映射)框架,而Flask中的flask-sqlalchemy其实就是在sqla ...
- 笔记-python-standard library-8.10 copy
笔记-python-standard library-8.10 copy 1. copy source code:Lib/copy.py python中的赋值语句不复制对象,它创建了对象和目 ...
- mac terminal基本命令
文件目录 首先要清楚几个文件目录: " / " :根目录 " ~ " :用户主目录的缩写.例如当前用户为esther,那么" ~ "展开来 ...
- itchat 动态注册
动态注册时可以选择将itchat.run()放入另一线程或使用configured_reply()方法处理消息. 两种方法分别是: # 使用另一线程,但注意不要让程序运行终止 import threa ...
- wim
wim 编辑 WIM是英文Microsoft Windows Imaging Format(WIM)的简称,它是Windows基于文件的映像格式.WIM 映像格式并非现在相当常见的基于扇区的映像格式, ...
- java web知识点
java web知识点 1.Java知识点 基本数据类型,面向对象,异常,IO,NIO,集合,多线程,JVM,高级特性. 2.web知识点 JSP,Serlvet,JDBC,Http 掌握Cookie ...
- Python框架之Django学习笔记(六)
模板 上篇博文学习了动态视图,但是,视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python 代码之中. def current_datetime(request): now = ...
- 从零开始到设计Python+Selenium自动化测试框架-如何开始
如何开始学习web ui自动化测试?如何选择一门脚本语言?选择什么自动化测试工具? 本人已经做测试快5年,很惭愧,感觉积累不够,很多测试都不会,三年多功能测试,最近两年才开始接触和学习自动化测试.打算 ...