https://leetcode.com/problems/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 .
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.
Example 1:
Input:version1
= "0.1",version2
= "1.1" Output: -1
Example 2:
Input:version1
= "1.0.1",version2
= "1" Output: 1
Example 3:
Input:version1
= "7.5.2.4",version2
= "7.5.3" Output: -1
代码:


class Solution { public: int compareVersion(string version1, string version2) { int len1 = version1.size(), len2 = version2.size(); int num1[100100], num2[100100]; int cnt1 = 0, cnt2 = 0; memset(num1, 0, sizeof(num1)); memset(num2, 0, sizeof(num2)); version1[len1] = '.'; len1 ++; version1[len1] = '\0'; version2[len2] = '.'; len2 ++; version2[len2] = '\0'; for(int i = 0; i < len1; i ++) { if(version1[i] != '.') for(int j = i; j < len1; j ++) { if(version1[j] != '.') continue; else { cnt1 ++; for(int k = i; k < j; k ++) num1[cnt1] = num1[cnt1] * 10 + (version1[k] - '0'); i = j; break; } } } for(int i = 0; i < len2; i ++) { if(version2[i] != '.') for(int j = i; j < len2; j ++) { if(version2[j] != '.') continue; else { cnt2 ++; for(int k = i; k < j; k ++) num2[cnt2] = num2[cnt2] * 10 + (version2[k] - '0'); i = j; break; } } } int flag = 0; int minn = min(cnt1, cnt2); for(int i = 1; i <= minn; i ++) { if(num1[i] == num2[i]) continue; else if(num1[i] > num2[i]) return 1; else return -1; } if(cnt1 == cnt2) return 0; else if(cnt1 > cnt2) { for(int i = minn + 1; i <= cnt1; i ++) { if(num1[i]) return 1; } } else { for(int i = minn + 1; i <= cnt2; i ++) { if(num2[i]) return -1; } } return 0; } };
第一次写的时候没想太多但是发现有前导零以及 $1.0$ 和 $1$ 这样的情况 所以以小数点为界限把里面的数字都挖出来然后进行比较 但是我这个写完就比较慢 晚上问一下 FH 有没有什么快一点的办法 /撇嘴