Leetcode_Compare Version Numbers

#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
    int compareVersion(string version1, string version2) {
		vector<int> v1;
		vector<int> v2;
		int p=0;
		/*方法一:OK*/
		/*for(int i=0;i<version1.size();++i)
		{
			if(version1[i]=='.')
			{
				string str(version1.begin()+p,version1.begin()+i);
				v1.push_back(stoi(str));
				p=i+1;
			}
		}
		string str1(version1.begin()+p,version1.end());
		v1.push_back(stoi(str1));

		p=0;
		for(int i=0;i<version2.size();++i)
		{
			if(version2[i]=='.')
			{
				string str(version2.begin()+p,version2.begin()+i);
				v2.push_back(stoi(str));
				p=i+1;
			}
		}
		string str2(version2.begin()+p,version2.end());
		v2.push_back(stoi(str2));*/

		/*方法二:NO  */
		/*
		for(int i=0;i<version1.size();++i)
		{
			if(version1[i]=='.'||i==version1.size()-1)//这样不可以,当最后一个字符不能复制进去
			{
				string str(version1.begin()+p,version1.begin()+i);
				v1.push_back(stoi(str));
				p=i+1;
			}
		}
		p=0;
		for(int i=0;i<version2.size();++i)
		{
			if(version2[i]=='.'||i==version2.size()-1)
			{
				string str(version2.begin()+p,version2.begin()+i);
				v2.push_back(stoi(str));
				p=i+1;
			}
		}*/

		/*方法三:OK  */
		p=0;
		for(int i=0;i<version1.size();++i)
		{	char a[64];
			if(version1[i]=='.')
			{
				memcpy(a,&version1[p],i-p);//没有结束符
				a[i-p]='\0';//非常重要
				v1.push_back(atoi(a));
				p=i+1;
			}
		}
		string str1(version1.begin()+p,version1.end());
		v1.push_back(stoi(str1));

		p=0;
		for(int i=0;i<version2.size();++i)
		{	char a[64];
			if(version2[i]=='.')
			{
				memcpy(a,&version2[p],i-p);//没有结束符
				a[i-p]='\0';//非常重要
				v2.push_back(atoi(a));
				p=i+1;
			}
		}
		string str2(version2.begin()+p,version2.end());
		v2.push_back(stoi(str1));

		/*int len1=v1.size();
		int len2=v2.size();
		if(len2<len1) return -1*compareVersion(version2, version1);
		int i=0;
		while(i<len1 && v1[i]==v2[i]) i++;
		if(i==len1){	//str1和str2前len1位都相等,则看看str2后面的len2-len1位是否都为0即可判断它们的大小
		int j=len2-1;
		while(j >= len1){
        if(v2[j--]!=0) return  -1;
		}
		return 0;
		}else{	   //str1和str2前len1位不都相等,直接判断第i位
		if(v1[i]<v2[i]) return -1;
		 else return 1;
		}*/

		int i;
		for( i=0;i<v1.size()&&i<v2.size();++i)
		{
			if(v1[i]>v2[i])
				return 1;
			if(v1[i]<v2[i])
				return -1;
		}
		if(i>=v1.size()&&i<v2.size())//当长度不相同而且前面全部相同的情况下,看较长的后面时候有大于0的出现
		{
			for(i=v1.size();i<v2.size();++i)
			{
				if(v2[i]>0)
					return -1;
			}
		}
		else if(i>=v2.size()&&i<v1.size())
			{
			for(i=v2.size();i<v1.size();++i)
			{
				if(v1[i]>0)
					return 1;
			}
		}
			return 0;
    }
};
int main()
{
	string v1,v2;
	Solution S;
	cin>>v1>>v2;
	cout<<S.compareVersion(v1,v2);
	return 0;
}

时间: 2024-10-05 17:47:22

Leetcode_Compare Version Numbers的相关文章

[LeetCode] Compare Version Numbers

Question: 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 

[leet code 165]Compare Version Numbers

1 题目 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 . cha

【Leetcode】:Compare Version Numbers

题目地址:https://oj.leetcode.com/problems/compare-version-numbers/ 题目描述: Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings ar

2016.5.21——Compare Version Numbers

Compare Version Numbers 本题收获: 1. 2. 题目: 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

leetcode 165. Compare Version Numbers

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 . charac

LeetCode OJ:Compare Version Numbers(比较版本字符串)

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 . characte

【LeetCode】165 - Compare Version Numbers

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 . characte

leetcode:Compare Version Numbers

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 . characte

Java for LeetCode 165 Compare Version Numbers

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 . characte