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:

  1. 0.1 < 1.1 < 1.2 < 13.37

注意可能会出现前置0的情况,所以分为小数点钱与小数点后转换成数字来判断,代码如下:

  1. class Solution {
  2. public:
  3. int compareVersion(string version1, string version2) {
  4. int i1, i2;
  5. int val1, val2;
  6. for(i1 = , i2 = ; i1 < version1.size() || i2 < version2.size(); ++i1, ++i2){
  7. val1 = ;
  8. for(; i1 < version1.size(); ++i1){
  9. if(version1[i1] == '.')
  10. break;
  11. val1 = val1 * + version1[i1] - '';
  12. }
  13. val2 = ;
  14. for(; i2 < version2.size(); ++i2){
  15. if(version2[i2] == '.')
  16. break;
  17. val2 = val2 * + version2[i2] - '';
  18. }
  19. if(val1 > val2)
  20. return ;
  21. else if(val1 < val2)
  22. return -;
  23. }
  24. return ;
  25. }
  26. };

LeetCode OJ:Compare Version Numbers(比较版本字符串)的更多相关文章

  1. Leetcode OJ : Compare Version Numbers Python solution

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

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

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

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

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

  4. leetcode:Compare Version Numbers

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

  5. 【leetcode】Compare Version Numbers

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

  6. 【leetcode】Compare Version Numbers(middle)

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

  7. Java for LeetCode 165 Compare Version Numbers

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

  8. Java [Leetcode 165]Compare Version Numbers

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

  9. Leetcode 165 Compare Version Numbers

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

  10. 【leetcode 字符串处理】Compare Version Numbers

    [leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Com ...

随机推荐

  1. 微信读书App来了 小伙伴们快去占榜吧

    微信读书App正式上线了,iOS版和Android版同时推出.届时将会出现像微信运动一样的霸榜小伙伴.资料显示,阅文集团成立于2014年1月,是腾讯文学和盛大文学联合成立的新公司.阅文集团成立后,会对 ...

  2. 002-es6字符串扩展

    1.字符串扩展 参考地址:http://es6.ruanyifeng.com/#docs/string 1.1.codePointAt() JavaScript 内部,字符以 UTF-16 的格式储存 ...

  3. tensorflow 的 tutorial 的卷积神经网络的例子 convolutional.py

    具体的网址在这里: https://github.com/tensorflow/tensorflow/tree/r0.12/tensorflow/models 一个卷积神经网络用于股票分析的例子:  ...

  4. Sqrt(x)

    这题没多大技巧性,只是牛顿迭代法多用于数值计算,这里出现有些意外.维基上有方法说明:http://zh.wikipedia.org/wiki/牛顿法 int sqrt(int x) { if (x = ...

  5. boost shared_ptr weak_ptr

    文档: http://www.boost.org/doc/libs/1_57_0/libs/smart_ptr/shared_ptr.htm shared_ptr构造有个原型 template< ...

  6. String与反序

    将String类型的字符串里的内容进行反序排列得到一个新的String类型字符串,下面提供两种方法实现: 法1.先将原String类型字符串转换为字符数组,通过字符数组来操作各个位上的单个字符,通过对 ...

  7. UEditor文本编辑器

    Ueditor是由百度web前端研发部开发所见即所得的编辑器,具有轻量,可定制,注重用户体验等特点.Ueditor基于BSD开源协议,除了具有代码精简.加载迅速的轻量级特质 外,还采用了分层理念,使开 ...

  8. Java基础_基本语法

    Java基本语法 一:关键字 在Java中有特殊含义的单词(50). 二:标志符 类名,函数名,变量名的名字的统称. 命名规则: 可以是字母,数字,下划线,$. 不能以数字开头. 见名之意. 驼峰规则 ...

  9. Spark机器学习4·分类模型(spark-shell)

    线性模型 逻辑回归--逻辑损失(logistic loss) 线性支持向量机(Support Vector Machine, SVM)--合页损失(hinge loss) 朴素贝叶斯(Naive Ba ...

  10. MyEclipse激活失败

    最近从MyEclipse2014升级MyEclipse2015,结果按照MyEclipse2014的方式激活2015总是失败,显示错误如下图所示: 反复实验,怎么也不能成功激活,最终找到方法 很多情况 ...