BM22. 比较版本号
描述
示例1
输入:
"1.1","2.1"
输出:
-1
说明:
version1 中下标为 0 的修订号是 "1",version2 中下标为 0 的修订号是 "2" 。1 < 2,所以 version1 < version2,返回-1示例2
输入:
"1.1","1.01"
输出:
0
说明:
version2忽略前导0,为"1.1",和version相同,返回0示例3
输入:
"1.1","1.1.1"
输出:
-1
说明:
"1.1"的版本号小于"1.1.1"。因为"1.1"的版本号相当于"1.1.0",第3位修订号的下标为0,小于1,所以version1 < version2,返回-1示例4
输入:
"2.0.1","2"
输出:
1
说明:
version1的下标2>version2的下标2,返回1示例5
输入:
"0.226","0.36"
输出:
1
说明:
226>36,version1的下标2>version2的下标2,返回1C++ 解法, 执行用时: 2ms, 内存消耗: 560KB, 提交时间: 2021-12-19
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 比较版本号 * @param version1 string字符串 * @param version2 string字符串 * @return int整型 */ int compare(string version1, string version2) { // write code here char* version1_p = (char*)version1.data(); int version1_len = version1.length(); int version1_idx = 0; vector<int> version1_nums; char* version2_p = (char*)version2.data(); int version2_len = version2.length(); int version2_idx = 0; vector<int> version2_nums; uint32_t version1_num_tmp=0; uint32_t version2_num_tmp=0; while(version1_idx<=version1_len){ if(version1_num_tmp!=0&&version1_idx==version1_len){ version1_nums.push_back(version1_num_tmp); } char c = *(version1_p+version1_idx); version1_idx++; if(c=='.'){ version1_nums.push_back(version1_num_tmp); version1_num_tmp = 0 ; } else if (c=='0'){ continue; } else{ version1_num_tmp=(version1_num_tmp*10)+(c-'0'); } } while(version2_idx<=version2_len){ if(version2_num_tmp!=0&&version2_idx==version2_len){ version2_nums.push_back(version2_num_tmp); } char c = *(version2_p+version2_idx); version2_idx++; if(c=='.'){ version2_nums.push_back(version2_num_tmp); version2_num_tmp = 0 ; } else if (c=='0'){ continue; } else{ version2_num_tmp=(version2_num_tmp*10)+(c-'0'); } } int maxnums = version1_nums.size()>version2_nums.size()?version1_nums.size():version2_nums.size(); for(int i=0;i<maxnums;i++){ if(version1_nums.size()<maxnums){ version1_nums.push_back(0); } if(version2_nums.size()<maxnums){ version1_nums.push_back(0); } } for(int i=0;i<maxnums;i++){ if(version1_nums[i]>version2_nums[i]){ return 1; } else if(version1_nums[i]<version2_nums[i]){ return -1; } } return 0; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 332KB, 提交时间: 2022-04-04
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 比较版本号 * @param version1 string字符串 * @param version2 string字符串 * @return int整型 */ int compare(string version1, string version2) { // write code here int len1=version1.length(); int len2=version2.length(); int i=0,j=0; int result=0; vector<char> arr1,arr2; while(i<len1&&j<len2){ while(i<len1&&version1[i]!='.'){ if(version1[i]!='0') arr1.push_back(version1[i]); i++; } while(j<len2&&version2[j]!='.'){ if(version2[j]!='0') arr2.push_back(version2[j]); j++; } int l1=arr1.size(); int l2=arr2.size(); if(l1>l2){ result=1; break; }else if(l1<l2){ result=-1; break; }else{ int k1=0,k2=0; bool label=false; while(k1<l1){ if(arr1[k1]>arr2[k2]){ result=1; label=true; break; }else if(arr1[k1]<arr2[k2]){ result=-1; label=true; break; } k1++; k2++; } if(label) break; arr1.clear(); arr2.clear(); i++; j++;//为了跳过刚刚那个‘.’ } } if(result==0){ while(i<len1){ if(version1[i]!='0'&&version1[i]!='.'){ result=1; break; } i++; } while(j<len2){ if(version2[j]!='0'&&version2[j]!='.'){ result=-1; break; } j++; } } return result; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-08-03
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 比较版本号 * @param version1 string字符串 * @param version2 string字符串 * @return int整型 */ int compare(string version1, string version2) { // write code here int v1=0; int v2=0; int j=0; for(int i=0;i<=version1.length();i++){ if(i==version1.length()||version1[i]=='.'){ while(j<version2.length()&&version2[j]!='.'){ v2=v2*10+(version2[j]-'0'); j++; } if(v1!=v2){return v1<v2?-1:1;} if(i==version1.length()&&j==version2.length()){return 0;} if(i==version1.length()){i--;} j++; v1=0;v2=0; }else{ v1=v1*10+(version1[i]-'0'); } } return 0; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-08-02
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 比较版本号 * @param version1 string字符串 * @param version2 string字符串 * @return int整型 */ int compare(string version1, string version2) { // write code here std::vector<std::string> strarry1; std::vector<std::string> strarry2; string q=version1; while(true){ int pos=q.find('.'); string p = q.substr(0,pos); q = q.substr(pos+1); strarry1.push_back(p); if(pos==-1) break; } q=version2; while(true){ int pos=q.find('.'); string p = q.substr(0,pos); q = q.substr(pos+1); strarry2.push_back(p); if(pos==-1) break; } int i=0,j=0; while(i<strarry1.size() && j<strarry2.size()){ string str1=strarry1[i++]; string str2=strarry2[j++]; int a = atoi(str1.c_str()); int b = atoi(str2.c_str()); if(a>b){return 1;} else if(a<b){return -1;} } while(i<strarry1.size()){ string str1=strarry1[i++]; int a = atoi(str1.c_str()); if(a!=0) return 1; } while(j<strarry2.size()){ string str1=strarry2[j++]; int a = atoi(str1.c_str()); if(a!=0) return -1; } return 0; } };
C 解法, 执行用时: 3ms, 内存消耗: 392KB, 提交时间: 2022-07-07
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 比较版本号 * @param version1 string字符串 * @param version2 string字符串 * @return int整型 * * C语言声明定义全局变量请加上static,防止重复定义 */ int* char_to_int(int len, char* version){ int tmp=0,value=0; int* out=calloc(len+1,sizeof(int)); for(int i=0;i<len;i++){ if(version[i]=='.'){ out[value++] = tmp; tmp=0; } else tmp = tmp*10 + (version[i]-'0'); } out[value++] = tmp; out[value]=-1; //增加一个结束标志 return out; } int compare(char* version1, char* version2 ) { // write code here int len1=strlen(version1); int len2=strlen(version2); int len = len1>len2 ? len1 : len2; int* out1=char_to_int(len1,version1); int* out2=char_to_int(len2,version2); int i=0,flag=0,sum=0;; for(;i<=len;i++){ if(out1[i]==-1){ if(out2[i]!=-1) flag=1; break; } else if(out2==-1){ if(out1[i]!=-1) flag=2; break; } else{ if(out1[i]>out2[i]) return 1; else if(out1[i]<out2[i]) return -1; } } if(flag==1){ for(int j=i+1;j<len;j++) //不能读到-1 sum+=out2[j]; if(sum>0) return -1; } else if(flag==2){ for(int j=i+1;j<len;j++) //不能读到-1 sum+=out1[j]; if(sum>0) return 1; } return 0; }