【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. Java中的命名规范到底是怎样的

    内容摘要:命名规范二,java中的方法名,对象名和字段名的第一个单词的首写字母应该小写,而后面的每个单词的首字母都应该小写 要想将java基础学的十分的牢固就必须将java中的命名规范掌握好了.俗话说 ...

  2. ansible upload

    # 链接地址:https://www.cnblogs.com/xiaoxiaoleo/p/6626299.html # synchronize: 从拉取远程服务器文件,需要加mode: pull # ...

  3. 【转载】程序猿转型AI必须知道的几件事!

    历史上AI火过两次,但是最终都已销声匿迹作为结束.这次AI大火的原因:AlphaGo 4比1战胜李世石,相对于一些外行人的恐慌和恐惧,其实很多业内人员在这场世纪之战结束后,都为人类点上了一个大大的赞. ...

  4. Teamwork-六月上旬心得体会

    六月上旬心得体会 在五月末的时候,老师针对我们团队的状况提出了几点建议和解决方案,而这半个月里,我们尝试性地运用了其中的几件工具与方法. 1.燃尽图与每日总结 我们采用的是<构建之法>书中 ...

  5. 【Python3】POP3协议收邮件

    初学Python3,做一个email的例子,虽然知道做的很渣渣,还是分享一下吧 POP3协议 POP3全称Post Official Protocol3,即邮局协议的第三个版本,它规定了怎样将个人计算 ...

  6. zmodem使用方法

    无论有xshell还是secureCRT连接linux的时. 默认都用一个zmodem可以帮助window和linux之间传输文件 很方便和实用的工具. 不过默认是无法使用的 需要安装lrzsz软件 ...

  7. 「Redis 笔记」数据类型

    REmote DIctionary Server(Redis),一个 key-value 存储系统. 数据类型 Redis 支持五种数据类型:string(字符串),hash(哈希),list(列表) ...

  8. 实验6 Bezier曲线生成

    1.实验目的: 了解曲线的生成原理,掌握几种常见的曲线生成算法,利用VC+OpenGL实现Bezier曲线生成算法. 2.实验内容: (1) 结合示范代码了解曲线生成原理与算法实现,尤其是Bezier ...

  9. 【技术累积】【点】【java】【5】Random和shuffle()

    闲聊 妈耶,又这么久没写了..不过最近写其他文章有点多啊... 今天用到Random这个类,竟然还要去查了下... 基本概念 Random类,背后是伪随机数(数学上的东西): 不是很理解,但是基本上而 ...

  10. HttpServletRequest二三事

    缘由 在项目中,闲来无聊写了个bug LOGGER.info("前端请求,request:{}",JSON.toJSONString(request)); 好像还好是吧,来我告诉你 ...