题目

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. Linux清空屏幕和清空当前输入的快捷键

    Linux清空屏幕和清空当前输入的快捷键 但是实际上...放弃当前的命令,命令行提示符跳到下一行的有效命令是ctrl + c

  2. Jenkins+Gitlab+Ansible自动化部署(三)

    接Jenkins+Gitlab+Ansible自动化部署(一)https://www.cnblogs.com/zd520pyx1314/p/10210727.html 和(二)https://www. ...

  3. setTimeout() 实现程序每隔一段时间自动执行

    定义和用法 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 语法 setTimeout(code,millisec) 参数 描述 code 必需.要调用的函数后要执行的 Ja ...

  4. Ubuntu 16.04 以太坊开发环境搭建

    今天我们来一步一步从搭建以太坊智能合约开发环境. Ubuntu16.04 安装ubuntu16.04.下载链接 //先update一下(或者换国内源再update) sudo apt-get upda ...

  5. NgStyle和NgIf控制HTML标签显示的区别

    通常web开发者会选择将元素样式属性display设为none来隐藏目标元素.采用这种方式,这些元素虽然不可见却仍然保存在DOM中,这样带来的好处是,如果元素不久就需要再次显示,组件不需要重新被初始化 ...

  6. javascript结合nodejs实现多文件上传

    前端文件上传功能比较依赖后端,所以第一步用nodejs实现一个供文件上传的功能接口. 因为本人对nodejs也是一知半解,所以刚开始的想法是像原始的ajax交互那样,获取上传文件的内容,然后再通过no ...

  7. ssh免密登录配置方法

    方法一 1.#ssh-keygen -t rsa 在客户端生成密钥对 把公钥拷贝给要登录的目标主机, 目标主机上将这个公钥加入到授权列表 #cat id_rsa.pub >> author ...

  8. UWP开发:自动生成迷宫&自动寻路算法(1)

    (1)前端篇 首先,我们创建一个新的Universal Windows Platform程序.这些小方块是通过GridView来罗列的,这样可以避免MainPaga.xaml的<Rectangl ...

  9. 打开某exe提示"应用程序无法启动,因为应用程序的并行配置不正确……"的解决方案

    本人在新安装好了的windows server 2008 r2 (64位)上运行“RefilesName V2.0(文件批量改名).exe”,结果提示: 应用程序无法启动,因为应用程序的并行配置不正确 ...

  10. java入门第一章——java开发入门

    习题解答 一.填空题 (p2)1.java的三个技术平台分别是(java SE.java EE.java ME)(标准.企业.小型) (p3)2.java程序的运行环境简称为(JRE)(开发环境-JD ...