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

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

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 自己写的代码跟discuss的最优代码几乎相同,不知道还有没有优化空间
int compareVersion(string version1, string version2) {    int i = 0, j = 0;    while (i < version1.length() || j < version2.length())    {        int a1 = 0;        int a2 = 0;                while (i < version1.length() && version1[i] != '.')            a1 = a1 * 10 + version1[i++] - '0';        while (j < version2.length() && version2[j] != '.')            a2 = a2 * 10 + version2[j++] - '0';        i++; j++;        if (a1 > a2)            return 1;        else if (a1 < a2)            return -1;    }    return 0;}

 

转载于:https://www.cnblogs.com/sdlwlxf/p/5143422.html

你可能感兴趣的文章
html标签
查看>>
vmware 中Linux系统怎么连接外网?
查看>>
js获取元素位置和style的兼容性写法
查看>>
博客写起来一周年了~
查看>>
bootstrap学习笔记<六>(表单二之按钮)
查看>>
springcloud(十三):Eureka 2.X 停止开发,但注册中心还有更多选择:Consul 使用详解...
查看>>
JS+CSS带数字和左右按钮可控制切换的图片幻灯
查看>>
Web API Request Content多次读取
查看>>
Debian VI高亮显示及注释颜色过灰暗更改办法
查看>>
面对对象基础
查看>>
Spark内存管理
查看>>
boneCP的连接管理
查看>>
关于Generating aspect-oriented Muti-Document Document summarization with event-aspect model
查看>>
KepServerEX读写三菱PLC,车间现场测试记录,带你了解【数据采集的困境】的前世与今生...
查看>>
消息机制
查看>>
使用java写一个简易的tomcat
查看>>
递归三:变态蛙跳台阶
查看>>
JVM 总体结构
查看>>
MySQL 教程分享
查看>>
php使用ffmpeg向视频中添加文字字幕
查看>>