【leetcode 字符串处理】Compare Version Numbers
【leetcode 字符串处理】Compare Version Numbers
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、分析
最后再比較两个容器中相应位置的数的大小。
当然须要考虑它们长度不同的情况。
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的更多相关文章
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【刷题-LeetCode】165 Compare Version Numbers
Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...
- LeetCode OJ:Compare Version Numbers(比较版本字符串)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- 【LeetCode】165 - Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- 【一天一道LeetCode】#165. Compare Version Numbers
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 165. Compare Version Numbers - LeetCode
Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...
- 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] Compare Version Numbers 字符串操作
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- Asura监控---AsuraMonitor,阿修罗监控开源
阿修罗Monitor是一个功能强大.灵活的监控系统. 系统安装简单,配置简单,相比zabbix, nagios,cacti,小米监控等都使用相当简单.只需要会写脚本,语言不限就可以实现任意监控需求. ...
- java中 抽象类和抽象方法
在面向对象中,所有的对象都是由类来描绘的,但是并不是所有的类都用来描绘对象的,当一个类并不能包含完整的信息来描绘一个具体的对象时,我们把这个类称为抽象类.抽象类除了不完整的描述一个对象之外,其他的功能 ...
- VS中的路径宏
说明$(RemoteMachine)设置为“调试”属性页上“远程计算机”属性的值.有关更多信息,请参见更改用于 C/C++ 调试配置的项目设置.$(References)以分号分隔的引用列表被添加到项 ...
- Eigen3
Eigen用源码的方式提供给用户使用,在使用时只需要包含Eigen的头文件即可进行使用. Eigen: C++开源矩阵计算工具——Eigen的简单用法 http://blog.csdn.net/aug ...
- Redis五种数据类型及应用场景
MySql+Memcached架构的问题 实际MySQL是适合进行海量数据存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都曾经使用过这样的架构,但随着业务数据量的不断增加 ...
- VirtualBox里如何正确安装增强工具(图文详解)
不多说,直接上干货! 找到 复制到
- Hadoop MapReduce编程 API入门系列之网页排序(二十八)
不多说,直接上代码. Map output bytes=247 Map output materialized bytes=275 Input split bytes=139 Combine inpu ...
- Android网络编程随想录(3)
大多数Android的app都会使用HTTP协议来发送和接收数据.在Android开发中,通常使用两种http客户端:一个是Apache的HttpClient,另一个是HttpURLConnectio ...
- TensorFlow-mnist
训练代码: from __future__ import absolute_import from __future__ import division from __future__ import ...
- Django[pronounced dʒ] installation on windows
1.Install python, download python windows installer from http://www.python.org/download/ and do inst ...