[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 ...
随机推荐
- 十六、MySQL LIKE 子句
MySQL LIKE 子句 我们知道在 MySQL 中使用 SQL SELECT 命令来读取数据, 同时我们可以在 SELECT 语句中使用 WHERE 子句来获取指定的记录. WHERE 子句中可以 ...
- Vue插槽
插槽内容 Vue实现一套内容分发的API, 这套API基于当前的web组件规范草案,将元素作为承载分发内容的出口. <navigation-link url="/profile&quo ...
- 微信小程序 onLoad 函数
小程序注册完成后,加载页面,触发onLoad方法. 页面载入后触发onShow方法,显示页面. 首次显示页面,会触发onReady方法,渲染页面元素和样式,一个页面只会调用一次. 当小程序后台运行或跳 ...
- HDU 5971 二分图判定
Wrestling Match Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- 动态规划:HDU-1203-0-1背包问题:I NEED A OFFER!
解题心得: 动态规划就是找到状态转移方程式,但是就本题0-1背包问题来说转移方程式很简单,几乎看模板就行了. 在本题来说WA了很多次,很郁闷,因为我记录v[i]的时候i是从0开始的,一些特殊数据就很尴 ...
- Robot Framework Webdriver For Firefox FQA
记录一下过程中使用的问题,希望大家碰到类似问题能够提高效率解决. 问题1.通过js脚本定位unieap框架网页中radio选项. 通过执行js脚本获取radio选项,并通过xpath路径点击. js脚 ...
- Java文件 ---流
分类 根据数据走向,分为输入流.输出流 根据处理的数据类型,分为字节流.字符流 字节流 可以处理所有类型的数据,如MP3.图片.文字.视频等.在读取时,读到一个字节就返回一个字节. 在Java中对应的 ...
- 如何拿到半数面试公司Offer——我的Python求职之路(转)
从八月底开始找工作,短短的一星期多一些,面试了9家公司,拿到5份Offer,可能是因为我所面试的公司都是些创业性的公司吧,不过还是感触良多,因为学习Python的时间还很短,没想到还算比较容易的找到了 ...
- sedgewick增量序列的希尔排序
#include<bits/stdc++.h> using namespace std; int s[3]={1,5,19}; void shellsort(int *a,int n){ ...
- 【Swap Nodes in Pairs】cpp
题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...