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

分析:题意为 比较两个版本数字version1和version2,值得注意的是.并不是代表小数点,例如:2.5表示第二个一级第五个二级版本

思路:可以进行逐位比较,把字符串转化成整型。

注意考虑test case:0.1 与 0.01  结果是:0.1>0.01

代码如下:
class Solution {
public:
int compareVersion(string version1, string version2) {
int v1, v2;
int t1 = 0, t2 = 0;
while (t1 < version1.length() || t2 < version2.length()) {
v1 = 0;
while (t1 < version1.length()) {
if (version1[t1] == '.') {
++t1;
break;
}
v1 = v1 * 10 + (version1[t1] - '0');
++t1;
}
v2 = 0;
while (t2 < version2.length()) {
if (version2[t2] == '.') {
++t2;
break;
}
v2 = v2 * 10 + (version2[t2] - '0');
++t2;
}
if (v1 > v2) return 1;
if (v1 < v2) return -1;
}
return 0;
}
};

 其他解法:

class Solution {
public:
int compareVersion(string version1, string version2) {
const char *p1 = version1.c_str()-1;
const char *p2 = version2.c_str()-1;
do{
int v1 = 0, v2 =0;
if (p1){
v1=atoi(p1+1);
p1 = strchr(p1+1, '.');
}
if (p2){
v2=atoi(p2+1);
p2 = strchr(p2+1, '.');
}
if (v1<v2) return -1;
if (v2<v1) return 1;
}while(p1||p2);
return 0;
}
};

 或:

class Solution {
public:
int compareVersion(string version1, string version2) {
int n = version1.size(), m = version2.size();
for (int i = 0, j = 0; i < n || j < m; i++, j++) {
size_t p = 0;
int a = ((i >= n) ? 0 : stoi(version1.substr(i), &p));
i += p;
int b = ((j >= m) ? 0 : stoi(version2.substr(j), &p));
j += p;
if (a > b) return 1;
if (a < b) return -1;
}
return 0;
}
};

  或:

class Solution {
public:
int compareVersion(string version1, string version2) {
istringstream iss1(version1), iss2(version2);
string token1, token2; while(!iss1.eof() || !iss2.eof()) {
getline(iss1, token1, '.'); getline(iss2, token2, '.'); if(stoi(token1) > stoi(token2)) return 1;
if(stoi(token1) < stoi(token2)) return -1; token1 = token2 = "0";
} return 0;
}
};

  

 

leetcode:Compare Version Numbers的更多相关文章

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

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

  2. [LeetCode] 165. Compare Version Numbers 比较版本数

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

  3. 【leetcode】Compare Version Numbers

    题目描述: Compare two version numbers version1 and version2. If version1 > version2 return 1, if vers ...

  4. 【leetcode】Compare Version Numbers(middle)

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

  5. ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java

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

  6. Java for LeetCode 165 Compare Version Numbers

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

  7. Java [Leetcode 165]Compare Version Numbers

    题目描述: Compare two version numbers version1 and version2.If version1 > version2 return 1, if versi ...

  8. Leetcode OJ : Compare Version Numbers Python solution

    Total Accepted: 12400 Total Submissions: 83230     Compare two version numbers version1 and version2 ...

  9. Leetcode 165 Compare Version Numbers

    题意:比较版本号的大小 有点变态,容易犯错 本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号 class Solution { public: int c ...

随机推荐

  1. 版本控制 - Git

    此篇blog只是对http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 研读后的总结,还 ...

  2. Leetcode#90 Subsets II

    原题地址 跟Subsets(参见这篇文章)类似. 但因为有重复元素,所以要考虑去重问题. 什么情况下会出现重复呢?比如S = {5, 5, 5},如果要选1个5,一共有C(3,1)=3种选法,即100 ...

  3. struct {0}初始化

    #include <iostream> struct MyStruct { MyStruct(int a) { a = b = ; } int a; int b; }; int main( ...

  4. 使用微信JSSDK自定义分享内容

    微信在6.0.2.58版本以后开始使用新的api,在Android系统中不能用以前的代码来自定义分享内容了. 现在自定义内容的方法走的是公众号的一套流程 1获取access_token 2得到toke ...

  5. Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 8

    Refer :http://www.cnblogs.com/mengfanrong/p/3745475.html Righ click on your project > properties ...

  6. POJ 1503 Integer Inquiry(大数相加,java)

    题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main ...

  7. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  8. ExtJs之工具栏及菜单栏

    先培养一下大概的感觉吧. 基本按书上都弄出来了. <!DOCTYPE html> <html> <head> <title>ExtJs</titl ...

  9. C Primer Plus之存储类、链接和内存管理

    存储时期即生存周期——变量在内存中保留的时间 变量的作用域和链接一起表明程序的哪些部分可以通过变量名来使用该变量. 注意:生存期和作用域是两个不同的概念. 作用域    作用域描述了程序中可以访问一个 ...

  10. Java Applet与Java Application的特点

    java application是应用程序,用于桌面开发,java applet是小应用程序,一般嵌入到网页里运行.applet一般用于B/S页面上作为插件式的开发,而application主要是桌面 ...