题目

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. 关于AQS——独占锁特性+共享锁实现(二)

    五.可中断获取锁的实现(独占锁的特性之一) 我们知道lock相较于synchronized有一些更方便的特性,比如能响应中断以及超时等待等特性,现在我们依旧采用通过学习源码的方式来看看能够响应中断是怎 ...

  2. Tensorflow版Faster RCNN源码解析(TFFRCNN) (1) VGGnet_test.py

    本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第1篇   VGGnet_test.py ----作者:Jiang Wu(吴疆),未经允许,禁止转载--- -- ...

  3. ruby 正则表达式 ruby-doc原文

    原文链接:http://www.ruby-doc.org/core-1.9.3/Regexp.html Regexp A Regexp holds a regular expression, used ...

  4. .net memcache

    非常感谢csdn及冷月宫主让我很快学会了.net操作 memcache 文章转自:http://download.csdn.net/detail/e_wsq/4358982 C#存取Memcache的 ...

  5. intelliJ idea 激活和配置

    1. 双击打开 Intellij IDEA 桌面快捷方式图标,如下图: 2. 点击 ok 按钮,进入激活页面: 3. 他提供的三种方式激活:账号激活,激活码激活,服务器地址激活,我们选择激活码激活,去 ...

  6. Mind must be master of the body, strong mind can separate the body from its suffering.

    Mind must be master of the body, strong mind can separate the body from its suffering.意志是身体的主人,有顽强的意 ...

  7. css样式优先级问题

    官方表述的CSS样式优先级如下: 通用选择器(*) < 元素(类型)选择器 < 类选择器 < 属性选择器 < 伪类 < ID 选择器 < 内联样式 那么,我们来举个 ...

  8. jquery笔记1--选择器

    一.概述:jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是“write ...

  9. bt5r3开启远程登录

    sshd-generate /etc/init.d/ssh restart

  10. ORACLE中能否找到未提交事务的SQL语句

      在Oracle数据库中,我们能否找到未提交事务(uncommit transactin)的SQL语句或其他相关信息呢?  关于这个问题,我们先来看看实验测试吧.实践出真知. 首先,我们在会话1(S ...