题解 | #比较版本号#
比较版本号
https://www.nowcoder.com/practice/2b317e02f14247a49ffdbdba315459e7
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 比较版本号 * @param version1 string字符串 * @param version2 string字符串 * @return int整型 */ int compare(string version1, string version2) { // write code here vector<int> t_ver1, t_ver2; int n = 0; istringstream sin1(version1); istringstream sin2(version2); string temp; while(getline(sin1, temp, '.')) { t_ver1.push_back(stoi(temp)); } while(getline(sin2, temp, '.')) { t_ver2.push_back(stoi(temp)); } int numer = 0; if(t_ver1.size() > t_ver2.size()) numer = t_ver1.size(); else numer = t_ver2.size(); for(int i = 0 ; i < numer; i++){ if(t_ver1.size() < i+1 ) t_ver1.push_back(0); if(t_ver2.size() < i+1 ) t_ver2.push_back(0);; if(t_ver1[i] > t_ver2[i]) return 1; else if(t_ver1[i] < t_ver2[i]) return -1; } return 0; } };