LeetCode OJ: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
注意可能会出现前置0的情况,所以分为小数点钱与小数点后转换成数字来判断,代码如下:
class Solution {
public:
int compareVersion(string version1, string version2) {
int i1, i2;
int val1, val2;
for(i1 = , i2 = ; i1 < version1.size() || i2 < version2.size(); ++i1, ++i2){
val1 = ;
for(; i1 < version1.size(); ++i1){
if(version1[i1] == '.')
break;
val1 = val1 * + version1[i1] - '';
}
val2 = ;
for(; i2 < version2.size(); ++i2){
if(version2[i2] == '.')
break;
val2 = val2 * + version2[i2] - '';
}
if(val1 > val2)
return ;
else if(val1 < val2)
return -;
}
return ;
}
};
LeetCode OJ:Compare Version Numbers(比较版本字符串)的更多相关文章
- Leetcode OJ : Compare Version Numbers Python solution
Total Accepted: 12400 Total Submissions: 83230 Compare two version numbers version1 and version2 ...
- ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java
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 version1.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 ...
- 【leetcode】Compare Version Numbers
题目描述: Compare two version numbers version1 and version2. If version1 > version2 return 1, if vers ...
- 【leetcode】Compare Version Numbers(middle)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Java for LeetCode 165 Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Java [Leetcode 165]Compare Version Numbers
题目描述: Compare two version numbers version1 and version2.If version1 > version2 return 1, if versi ...
- Leetcode 165 Compare Version Numbers
题意:比较版本号的大小 有点变态,容易犯错 本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号 class Solution { public: int c ...
- 【leetcode 字符串处理】Compare Version Numbers
[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...
随机推荐
- Acheron一期SVN地址
svn://10.0.0.100/project/Acheron/2.0/SourceCode tailf 命令 http://web2py.com/books/default/chapter/29/ ...
- 运行hadoop自带的wordcount例子程序
1.准备文件 [root@master ~]# cat input.txt hello java hello python hello c hello java hello js hello html ...
- mysql增量恢复的一个实例操作
通过防火墙禁止web等应用向主库写数据或者锁表,让主库暂时停止更新,然后进行恢复 模拟整个场景 1.登录数据库 [root@promote 3306]# mysql -uroot -S /data/3 ...
- delphi webbrowser post自动登录
delphi webbrowser post自动登录 var EncodedDataString: WideString; PostData: OleVariant; Headers: ...
- Typecho部署安装
此文章已经在这里上. 如果您看到这篇文章,表示您的 blog 已经在digitalocean.com安装成功.下面说下安装的步骤,此文章都是在digitalocean.com的centos上成功安装: ...
- 转:使用log4net完成程序异常日志记录(使用SQLite数据库记录和普通文本记录)
http://www.cnblogs.com/kyo-yo/archive/2010/06/11/use-log4net-to-log-exception.html 在前端时间开发的时候由于需要将异常 ...
- SVN插件下载地址及更新地址
SVN插件下载地址及更新地址,你根据需要选择你需要的版本.现在最新是1.8.xLinks for 1.8.x Release:Eclipse update site URL: http://subcl ...
- git-bash使用ctrl C无法终止nodemon的执行
原因: git的bug 解决:git版本降级为2.10.0好了
- hadoop源码分析
hadoop 源代码分析(一) Google 的核心竞争技术是它的计算平台.HadoopGoogle的大牛们用了下面5篇文章,介绍了它们的计算设施. GoogleCluster:http://rese ...
- 检索并打印一个DNS主机条目
检索并打印一个DNS主机条目的 C 程序 --- Linux/Unix /*************************************************************** ...