首页 > 代码库 > LeetCode Compare Version Numbers
LeetCode Compare Version Numbers
class Solution {public: int compareVersion(string version1, string version2) { vector<int> v1 = getVersionParts(version1); vector<int> v2 = getVersionParts(version2); int i = 0, j = 0; int len1 = v1.size(); int len2 = v2.size(); while (i < len1 && j < len2) { if (v1[i] < v2[i]) { return -1; } else if (v1[i] > v2[i]) { return 1; } i++, j++; } int r = 0; while (i < len1) { r += v1[i++]; } if (r != 0) { return 1; } r = 0; while (j < len2) { r += v2[j++]; } if (r != 0) { return -1; } return 0; } vector<int> getVersionParts(string& version) { vector<int> parts; int len = version.length(); int v = 0; char d = ‘\0‘; for (int i=0; i<len; i++) { d = version[i]; if (d == ‘.‘) { parts.push_back(v); v = 0; continue; } v = v * 10 + d - ‘0‘; } if (d != ‘.‘) { parts.push_back(v); } return parts; }};
水一发,改进一下贴一个简化版的:
class Solution {public: int compareVersion(string version1, string version2) { int part1 = 0; int part2 = 0; int len1 = version1.length(); int len2 = version2.length(); int i = 0, j = 0; while (i < len1 || j < len2) { char ch; while (i < len1 && (ch = version1[i++]) != ‘.‘) { part1 += ch - ‘0‘ + part1 * 10; } while(j < len2 && (ch = version2[j++]) != ‘.‘) { part2 += ch - ‘0‘ + part2 * 10; } if (part1 > part2) { return 1; } else if (part1 < part2) { return -1; } else { // part equal, continue to compare next version part } part1 = part2 = 0; } return 0; }};
LeetCode Compare Version Numbers
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。