LeetCode:Compare Version Numbers

52 篇文章 0 订阅
原题:
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


解题思路:比较两个版本号的大小,比较坑的是可能有多个"."号,比如:01.23.323 与0.0.102,但有一个简单的思路----直接用strtok将原字符串分割,并为每个分割后的子字符串分配空间,然后利用atoi函数将子字符串转化为数字,这样以此从左到右比较数字的大小就可以了。首先介绍一下c语言strtok函数和atoi函数的用法。


1、strtok函数

头文件:#include <string.h>

函数原型: char * strtok (char * str, const char * delimiters); 

功能:strtok()用来将字符串分割成一个个片段。参数str指向欲分割的字符串,参数delimiters则为分割字符串,分割字符串可以有多个字符;当strtok()在参数str的字符串中发现到参数delimiters的分割字符时则会将该字符改为'\0'字符。在第一次调用时,strtok()必需给予参数str字符串(str所指向的字符串必须已经分配地址,必须是数组名,或者已用malloc分配地址),往后的调用则将参数str设置成NULL,即调用形式为:strtok(NULL,delimeters),每次调用成功则返回下一个分割后的字符串指针,直到第一次传入的字符串分割完毕为止。 

返回值:返回下一个分割后的字符串指针,如果已无从分割则返回NULL。

2、atoi函数

头文件:#include<stdlib.h>

函数原型:int atoi(const char *str);

功能:atoi() 函数会扫描参数 str 字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过 isspace() 函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。类似的函数有:long atol(const char* str); long long atoll(const char* str);
返回值:转换后的整数。

下面是Compare Version Numbers题目的源代码: 

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int compareVersion(char *version1, char *version2){
	unsigned count1=0,count2=0;
	unsigned i;
	char *temp_v1,*temp_v2;
	char **v1_list,**v2_list;
	unsigned len1 = strlen(version1);
	unsigned len2 = strlen(version2);
	int t1,t2,flag;

	temp_v1 = (char*)malloc(sizeof(char)*len1); //将version1和version2放到自己申请的数组里,直接对version1使用strtok会有问题
	temp_v2 = (char*)malloc(sizeof(char)*len2);
	strcpy(temp_v1,version1);
	strcpy(temp_v2,version2);

	for(i=0;i<len1;i++){	//计算"."的个数
		if(version1[i]=='.'){
			count1++;
		}
	}
	for(i=0;i<len2;i++){
		if(version2[i]=='.'){
			count2++;
		}
	}
	v1_list = (char **)malloc(sizeof(char*)*(count1+1)); //子字符串的个数为"."的个数加1
	v2_list = (char **)malloc(sizeof(char*)*(count2+1));

	v1_list[0] = strtok(temp_v1,".");   //放入第一个子字符串
	for(i=1;i<=count1;i++){             //放入后续字符串
		v1_list[i] = strtok(NULL,".");
	}
	
	v2_list[0] = strtok(temp_v2,".");
	for(i=1;i<=count2;i++){
		v2_list[i] = strtok(NULL,".");
	}

	for(i=0;i<=count1&&i<=count2;i++){ //从左到右比较数字大小
		t1 = atoi(v1_list[i]);
		t2 = atoi(v2_list[i]);
		if(t1>t2){ //t1大,返回1
			return 1;
		}else if(t1<t2){
			return -1;
		}
	}
	if(i==count1+1 && i==count2+1){        //相等
		return 0;
	}
	flag = 0;
	if(i<=count1){           //判断余下的是不是全0,如1.00
		while(i<=count1){
			if(atoi(v1_list[i])!=0){
				flag = 1;
			}
			i++;
		}
		if(flag==1){
			return 1;
		}else{
			return 0;
		}
	}
	if(i<=count2){
		while(i<=count2){
			if(atoi(v2_list[i])!=0){
				flag = 1;
			}
			i++;
		}
		if(flag==1){
			return -1;
		}else{
			return 0;
		}
	}
}
int main(){
	printf("%d\n",compareVersion("0.1.0","0.1"));
	return EXIT_SUCCESS;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值