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. C++ lambda 表达式 简介

    自己根据对lambda表达式的理解,做了一套ppt简单介绍

  2. CentOS7安装Nginx、MySQL、PHP

    之前才网上找了好多文章,但是配置总会出错,后来傻傻的发现官方文档都有的,当然配合网上文章说明更好,因此本文只说一个大概 安装PHP 官方配置 配置用户和用户组,需要有根目录权限 vim /usr/lo ...

  3. Mysql主从数据同步cheksum问题

    做主从同步时出现问题,show slave status显示错误: Last_IO_Error: Got fatal error from master when reading data from ...

  4. 【PHP】常用的PHP正则表达式收集整理

    匹配中文字符的正则表达式: [\u4e00-\u9fa5]评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^\x00-\xff]评注:可以用来计算字符串的长度 ...

  5. Codeforces Round #462 (Div. 2) A Compatible Pair

    A. A Compatible Pair time limit per test1 second memory limit per test256 megabytes Problem Descript ...

  6. Codeforces:68A-Irrational problem(暴力大法好)

    A- Irrational problem p Time Limit: 2000MS Memory Limit: 262144K 64bit IO Format: %I64d& %I64 De ...

  7. 搭建Data Mining环境(Spark版本)

    前言:工欲善其事,必先利其器.倘若不懂得构建一套大数据挖掘环境,何来谈Data Mining!何来领悟“Data Mining Engineer”中的工程二字!也仅仅是在做数据分析相关的事罢了!此文来 ...

  8. IOS开发---菜鸟学习之路--(二十四)-iOS7View被导航栏遮挡问题的解决

    (此文为复制帖,原文地址为:http://blog.sina.com.cn/s/blog_a8192bdd0101af40.html) self.navigationController.naviga ...

  9. 35、键盘布局的tableLayout备份

    <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_conte ...

  10. Win7系统安装MySQL5.5.21图解

    Win7系统安装MySQL5.5.21图解 大家都知道MySQL是一款中.小型关系型数据库管理系统,很具有实用性,对于我们学习很多技术都有帮助,前几天我分别装了SQL Server 2008和Orac ...