题目

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

分析

方法一:

采用字符串比较;此题中的版本比较,需要将.号前的部分与第二部分分开比较;

方法二:

将版本号的字符串转化为整数;

AC代码

class Solution {
public:
/*方法一:字符串比较;如果version1 > version2 则返回1 相等返回0 反之返回-1*/
int compareVersion1(string version1, string version2) {
if (version1.empty() && version2.empty())
return 0;
else if (version2.empty())
return 1;
else if (version1.empty())
return -1;
else
{
//分别得到版本号的第一部分和第二部分
int pos = 0;
for (int i = 0; version1[i] != '\0' && version1[i] != '.'; ++i)
{
++pos;
}//for
string fir_version1;
string sec_version1;
if (pos == version1.length())
{
fir_version1 = version1;
sec_version1 = "";
}
else{
fir_version1 = version1.substr(0, pos);
sec_version1 = version1.substr(pos + 1, version1.length() - pos - 1);
}
//要略过第一部分版本号的开头的0
pos = 0;
while (fir_version1[pos] != '\0' && fir_version1[pos] == '0')
{
++pos;
}//while
if (fir_version1[pos] == '\0')
fir_version1 = "0";
else
fir_version1 = fir_version1.substr(pos, fir_version1.length() - pos); pos = 0;
for (int i = 0; version2[i] != '\0' && version2[i] != '.'; ++i)
{
++pos;
}//for
string fir_version2;
string sec_version2;
if (pos == version2.length())
{
fir_version2 = version2;
sec_version2 = "";
}
else{
fir_version2 = version2.substr(0, pos);
sec_version2 = version2.substr(pos + 1, version2.length() - pos - 1);
}//else pos = 0;
while (fir_version2[pos] != '\0' && fir_version2[pos] == '0')
{
++pos;
}//while
if (fir_version2[pos] == '\0')
fir_version2 = "0";
else
fir_version2 = fir_version2.substr(pos, fir_version2.length() - pos); if (strcmp(fir_version1.c_str(), fir_version2.c_str()) != 0)
return strcmp(fir_version1.c_str(), fir_version2.c_str());
else
return strcmp(sec_version1.c_str(), sec_version2.c_str());
}
} //方法二:化为整数比较
int compareVersion(string version1, string version2) {
int val1, val2;
int idx1 = 0, idx2 = 0;
while (idx1 < version1.length() || idx2 < version2.length()) {
val1 = 0;
while (idx1 < version1.length()) {
if (version1[idx1] == '.') {
++idx1;
break;
}
val1 = val1 * 10 + (version1[idx1] - '0');
++idx1;
}
val2 = 0;
while (idx2 < version2.length()) {
if (version2[idx2] == '.') {
++idx2;
break;
}
val2 = val2 * 10 + (version2[idx2] - '0');
++idx2;
}
if (val1 > val2) return 1;
if (val1 < val2) return -1;
}
return 0;
}
};

GitHub测试程序源码

LeetCode(165) Compare Version Numbers的更多相关文章

  1. LeetCode(68)-Compare Version Numbers

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

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

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

  3. LeetCode(2)Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  4. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. 165. Compare Version Numbers - LeetCode

    Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: p ...

  6. 【刷题-LeetCode】165 Compare Version Numbers

    Compare Version Numbers Compare two version numbers version1 and version2. If *version1* > *versi ...

  7. 2016.5.21——Compare Version Numbers

    Compare Version Numbers 本题收获: 1.字符串型数字转化为整型数字的方法:s[i] - '0',( 将字母转化为数字是[i]-'A'   ) 2.srt.at(),substr ...

  8. 新概念英语(1-65)Not a Baby

    新概念英语(1-65)Not a Baby Does Jill take the key to the front door? A:What are you going to do this even ...

  9. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

随机推荐

  1. ASP.NET Core Linux

    环境说明 CentOS / 7.1 (64bit) (Linux操作系统) 3MySQL5.7(网站应用数据库) .NET Core SDK 2.0.0(网站应用环境) Nginx(反向代理服务器) ...

  2. 059 Spiral Matrix II 旋转打印矩阵 II

    给出正整数 n,生成正方形矩阵,矩阵元素为 1 到 n2 ,元素按顺时针顺序螺旋排列.例如,给定正整数 n = 3,应返回如下矩阵:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6 ...

  3. Windows和Ubuntu使用网线直连搭建局域网

    1.Windows下的配置:右键右下角的网络图标(或者右键网络→属性)→更改适配器设置→以太网→右键属性→TCP/IPv4→IP地址(192.168.1.3)→子网掩码(255.255.255.0)→ ...

  4. (转)企业配置sudo命令用户行为日志审计

    原文:https://www.cnblogs.com/Csir/p/6403830.html?utm_source=itdadao&utm_medium=referral 第15章 企业配置s ...

  5. js得到当前页面的url信息

    所有的代码都是可用,而且附了图片的,不过是直接用我自己的文章地址,所以有些显示的有点奇怪. 大家可以找个网址试试代码是否可行. 1,设置或获取对象指定的文件名或路径. console.log(wind ...

  6. 【转载】【MVC 学习 Razor语法】

    Razor是MVC3中才有的新的视图引擎.我们知道,在ASP.NET中,ASPX的视图引擎依靠<%和%>来调用C#指令.而MVC3以后有了一套新的使用@标记的Razor语法,使用起来更灵活 ...

  7. NIO基础之Buffer

    java.io 核心概念是流,即面向流的编程,在java中一个流只能是输入流或者输出流,不能同时具有两个概念. java.nio核心是 selector.Channel.Buffer ,是面向缓冲区( ...

  8. 韦东山笔记之安装arm-linux-gcc交叉编译环境详细步骤。

    1在关盘主目录tools下复制arm-linux-gcc-3.4.5-glibc-2.3.6.tar.bz2到虚拟机上     解压 tar xjf arm-linux-gcc-3.4.5-glibc ...

  9. 《移动Web前端高效开发实战》笔记3--代码检查任务

    在项目的开发过程中,统一的代码风格对于项目的可协作性以及可维护性来说相当重要,因此可以采用一些插件来进行代码风格的检查. 本例中的源文件包含两类:Sass文件和采用ECMAScript 6规范的Jav ...

  10. [ros]编译ORBSLAM2时候,ros路径问题

    CMake Error at CMakeLists.txt:2 (include): include could not find load file: /core/rosbuild/rosbuild ...