【leetcode 字符串处理】Compare Version Numbers


@author:wepon
@blog:http://blog.csdn.net/u012162613

1、题目

Compare two version numbers version1 and version1.

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

2、分析

题意:题意非常清晰。就是比較“版本”大小。给定的版本version1和version2是字符串类型的。当version1>version2的时候。返回1。反之返回-1。


这道题属于细节处理题,除了字符串处理繁琐一点之外没有什么。

解题思路:先分别将version1、version2字符串按'.'切割成多个子串,每一个子串转化成整型存入容器。

最后再比較两个容器中相应位置的数的大小。

当然须要考虑它们长度不同的情况。


注意点:
(1)version的形式:10.23.1、01.23、1.2.3.4....诸如此类
(2)字符串转化成整型,我的程序中直接用库函数stoi()

3、代码

class Solution {
public:
int compareVersion(string version1, string version2) {
vector<int> result1=getInt(version1);
vector<int> result2=getInt(version2);
int len1=result1.size();
int len2=result2.size();
if(len2<len1) return -1*compareVersion(version2, version1);
int i=0;
while(i<len1 && result1[i]==result2[i]) i++; if(i==len1){ //str1和str2前len1位都相等,则看看str2后面的len2-len1位是否都为0就可以推断它们的大小
int j=len2-1;
while(j >= len1){
if(result2[j--]!=0) return -1;
}
return 0;
}else{ //str1和str2前len1位不都相等。直接推断第i位
if(result1[i]<result2[i]) return -1;
else return 1;
}
}
private:
//将version字符串按'.'拆成多个。转化为整型放入容器
vector<int> getInt(string version){
vector<int> result;
int len=version.size();
int pre=0;
for(int i=0;i<len;i++){
if(version[i]=='.'){
string str(version.begin()+pre,version.begin()+i); //注意这样的初始化形式,左闭右开,即str不包含version[version.begin()+i]
result.push_back(stoi(str));
pre=i+1;
}
}
string str(version.begin()+pre,version.end());
result.push_back(stoi(str));
return result;
}
};

【leetcode 字符串处理】Compare Version Numbers的更多相关文章

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

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

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

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

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

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

  4. 【LeetCode】165 - Compare Version Numbers

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

  5. 【一天一道LeetCode】#165. Compare Version Numbers

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  6. 165. Compare Version Numbers - LeetCode

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

  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] Compare Version Numbers 字符串操作

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

随机推荐

  1. Linux Shell Scripting Cookbook 读书笔记 7

    ping, du, ps, kill, 收集系统信息 判断网络中哪些主机是活动主机 #!/bin/bash for ip in 10.215.70.{1..255}; do ( ping $ip -c ...

  2. POJ 3275 Floyd传递闭包

    题意:Farmer John想按照奶牛产奶的能力给她们排序.现在已知有N头奶牛(1 ≤ N ≤ 1,000).FJ通过比较,已经知道了M(1 ≤ M ≤ 10,000)对相对关系.每一对关系表示为&q ...

  3. IO流读取文件内容时,出现空格的问题(未找到原因)

    import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOExceptio ...

  4. &nbsp; 等等空格用法

    平时经常用到 空格转移字符,记住一个   表示一个字符就可以了. Non-Breaking SPace 记住它是什么的缩写,更有助于我们记忆和使用.下面的字符转义自己视图翻译一下. 记录一下,空格的转 ...

  5. (转载)android开发常见编程错误总结

    首页 › 安卓开发 › android开发 android开发常见编程错误总结 泡在网上的日子 / 文 发表于2013-09-07 13:07  第771次阅读 android,异常 0 编辑推荐:稀 ...

  6. Java单例模式 多种实现方式

    一:通过静态私有成员实现单例模式 (1):私有化构造函数 (2):new静态实例属性对象,加锁. 单例类: package SinglePag; /* * 构造函数私有化,结合锁+静态的概念 实现单例 ...

  7. 3D特征:关于HFM和HBB

    1.HBB    三维绑定框 (1): 要用到HBB,定义还不太清楚,来自于 VALVE Developer Community (https://developer.valvesoftware.co ...

  8. Django 了解

    Django是一个开放源代码的Web应用框架 Django也是一个基于 MVC 构造的框架. 但是在Django中,控制器接受用户输入的部分由框架自行处理,所以 Django 里更关注的是模型(Mod ...

  9. 原生ajax的get和post方法封装

    get 方法 function serialize (data) { if (!data) { return ''; } var paris = []; for (var key in data) { ...

  10. Project Euler 11 Largest product in a grid

    题意:在这个20×20方阵中,四个在同一方向(从下至上.从上至下.从右至左.从左至右或者对角线)上相邻的数的乘积最大是多少? 思路:暴力去枚举以 ( x , y ) 为中心拓展的四个方向 /***** ...