比较版本号-双指针比较
165. 比较版本号 - 力扣(LeetCode)
Solution
双指针比较法的模板
int i=0,j=0;
while(i<n1||j<n2){如果i和j都没有越界,比较 x[i]和y[j],也就是比较i指向的元素和j指向的元素i++;j++;
}
class Solution {
public:int compareVersion(string version1, string version2) {vector<string> v1;vector<string> v2;string temp;for (int i = 0; i < version1.size(); ++i) {char c = version1[i];if (c != '.')temp += c;else if (c == '.') {v1.push_back(temp);temp = "";}}v1.push_back(temp);temp = "";for (int i = 0; i < version2.size(); ++i) {char c = version2[i];if (c != '.')temp += c;else if (c == '.') {v2.push_back(temp);temp = "";}}v2.push_back(temp);temp = "";int n1 = v1.size();int n2 = v2.size();int i = 0, j = 0;while (i < n1 || j < n2) {int num1 = (i < n1) ? stoi(v1[i]) : 0;int num2 = (j < n2) ? stoi(v2[j]) : 0;if (num1 != num2) {return num1 > num2 ? 1 : -1;}i++;j++;}return 0;}
};