博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Leetcode】【Easy】Compare Version Numbers
阅读量:4589 次
发布时间:2019-06-09

本文共 1543 字,大约阅读时间需要 5 分钟。

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

 

解题:

题目的意思是,比较版本号的大小,2版本比1版本大,而2.10版本比2.5版本大;

分别取出小数点前和小数点后的数字做比较;

代码:

1 class Solution { 2 public: 3     int compareVersion(string version1, string version2) { 4         int len1 = version1.size();  5         int len2 = version2.size(); 6         int num1 = 0; 7         int num2 = 0; 8         int i = 0;  9         int j = 0;10         while(i < len1 || j < len2) {11             while (i < len1 && version1[i] != '.') {12                 num1 = num1 * 10 + (version1[i] - '0');13                 i++;14             }15 16             while (j < len2 && version2[j] != '.') {17                 num2 = num2 * 10 + (version2[j] - '0');18                 j++;19             }20 21             if(num1 > num2) 22                 return 1;23             else if (num1 < num2) 24                 return -1;25 26             num1 = 0;27             num2 = 0;28             i++;29             j++;30         }31 32         return 0;33     }34 };

 

 

转载于:https://www.cnblogs.com/huxiao-tee/p/4759432.html

你可能感兴趣的文章
spring4的新特性---泛型注入
查看>>
在linux上安装MySQL数据库,并简单设置用户密码,登录MySQL
查看>>
CallBack
查看>>
Codeforces Round #453
查看>>
第二次试验报告
查看>>
(转载)C# ListView用法详解
查看>>
R语言入门心得(1) -- 下载与安装
查看>>
正则表达式
查看>>
C#中linq报“Character literal must contain exactly one character”的错误提示
查看>>
eclipse中设置自动生成的author,date等注释
查看>>
【插件开发】—— 12 GEF入门
查看>>
消息提示
查看>>
isinstance(obj1,class) 可以判断前者是否是后者的实例
查看>>
数据库中怎么查询所有的表名
查看>>
Andrew Ng-ML-第十六章-异常检测
查看>>
全概率+贝叶斯[转载]
查看>>
浅析Cross Origin Resource Sharing
查看>>
Intent详解
查看>>
JS:九宫格抽奖转盘实例
查看>>
【洛谷P1801】黑匣子_NOI导刊2010提高(06)
查看>>