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 ...
随机推荐
- C#前后端交互
后台接收 方式1: Request.Form[""] post Request.QueryString[""] get System.Web.HttpConte ...
- Kattis - pizzahawaii 【状态标记】
Kattis - pizzahawaii [状态标记] Description You are travelling in a foreign country. Although you are al ...
- PAT 天梯赛 L1-017. 到底有多二 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-017 AC代码 #include <iostream> #include <cstdio&g ...
- NGUI如何使用汉字
1:准备好字体文件,就是ttf后缀名的那些.. 2:在第一个红线部分,将下拉框选择为Unity,在后面的字体里面选择第一步准备好的字体. 3:创建UILabel,widget里面的Color才是字体的 ...
- VMWare 安装时报错 tools-windows.msi failed报错解决办法
1.我用的是7.1.3版本的,到官方网站上下载这个版本的tools安装包 http://softwareupdate.vmware.com/cds/vmw-desktop/ws/7.1.3/32428 ...
- Javascript中类型: undefined, number ,string ,object ,boolean
var a1; var a2 = true;var a3 = 1;var a4 = "Hello";var a5 = new Object();var a6 = null;var ...
- Sybase:解锁
Sybase:解锁 Sql代码: --查询锁表 sp_iqlocks --解除锁定 drop connection[连接序号]
- Arrays.asList()与toArray()
Arrays.asList() 使用Arrays.asList()把数组转换成集合时,不能使用用于修改集合的方法(例如add.remove.clear),这将导致跑出UnsupportOperatio ...
- MySQL数据库的主从同步复制配置
一.主从同步机制原理 MYSQL主从同步是在MySQL主从复制(Master-Slave Replication)基础上实现的,通过设置在Master MySQL上的binlog(使其处于打开状态), ...
- MySQL性能优化之max_connections参数
很多开发人员都会遇见”MySQL: ERROR 1040: Too many connections”的异常情况,造成这种情况的一种原因是访问量过高,MySQL服务器抗不住,这个时候就要考虑增加从服务 ...