leecode 解题总结:165. Compare Version Numbers

#include
#include
#include
#include
using namespace std;
/*
问题:
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.2.5。我记得之前就有版本号: 7.2.0.301,7.1.0.302.
应该就是这种,需要按照"."分割字符串,每一部分转化为整数比较即可


注意"0"这种


输入:
7.2.0.301 7.2.1.100
7.2.1.100 7.2.0.301
7.2.001.1 7.2.001.1
0.1 1.1
1.2 13.37
1 1.1
1 1.0.0
输出:
-1
1
0
-1
-1
-1
0


关键:
1
报错:Input:"1","1.1",Output:1,Expected:-1。如果比较到后面一个数为空,
遍历令一个数中剩余部分,如果全为0,则相等,否则,剩余的数对应版本号大


2需要按照"."分割,逐个比较
*/


class Solution {
public:
vector split(string str , string splitStr)
{
vector result;
if(str.empty())
{
return result;
}
if(splitStr.empty())
{
result.push_back(str);
return result;
}
int beg = 0;
size_t pos = str.find(splitStr , beg);
string partialStr;
while(string::npos != pos)
{
//切分字符串
partialStr = str.substr(beg , pos - beg);
if(!partialStr.empty())
{
result.push_back(partialStr);
}
beg = pos + splitStr.length();//起始位置等于pos加上长度
pos = str.find(splitStr , beg);
}
//切分最后一次的结果
partialStr = str.substr(beg , pos - beg);
if(!partialStr.empty())
{
result.push_back(partialStr);
}
return result;
}


    int compareVersion(string version1, string version2) {
        if(version1.empty() && version2.empty())
{
return 0;
}
if(version1.empty())
{
return -1;
}
if(version2.empty())
{
return 1;
}
vector nums1 = split(version1 , string("."));
vector nums2 = split(version2 , string("."));
if(nums1.empty())
{
return -1;
}
if(nums2.empty())
{
return 1;
}
int size1 = nums1.size();
int size2 = nums2.size();
int minSize = min(size1 , size2);
int num1;
int num2;
for(int i = 0 ; i < minSize ; i++)
{
num1 = atoi(nums1.at(i).c_str());
num2 = atoi(nums2.at(i).c_str());
//如果第一个数大于第二个数,直接返回
if(num1 > num2)
{
return 1;
}
else if(num1 < num2)
{
return -1;
}
}
//如果全部比完,且长度相同,返回0
if(size1 == minSize && size2 == minSize)
{
return 0;
}
//第一个版本号后面还有元素,说明它是较小的,返回-1
//取出剩余部分数的每一个元素,如果全0,就相等,否则剩余数字对应版本号较大
if(size1 > size2)
{
for(int i = size2 ; i < size1 ; i++)
{
num1 = atoi(nums1.at(i).c_str());
if(num1 != 0)
{
return 1;
}
}
return 0;
}
else
{
for(int i = size1 ; i < size2 ; i++)
{
num2 = atoi(nums2.at(i).c_str());
if(num2 != 0)
{
return -1;
}
}
return 0;
}
    }
};


void print(vector& result)
{
if(result.empty())
{
cout << "no result" << endl;
return;
}
int size = result.size();
for(int i = 0 ; i < size ; i++)
{
cout << result.at(i) << " " ;
}
cout << endl;
}


void process()
{
string version1;
string version2;
Solution solution;
int result;
while(cin >> version1 >> version2)
{
result = solution.compareVersion(version1 , version2);
cout << result << endl;
}
}


int main(int argc , char* argv[])
{
process();
getchar();
return 0;
}



你可能感兴趣的:(leecode)