本文主要是介绍165. 比较版本号——字符串,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
class Solution {
public:int compareVersion(string version1, string version2) {int vtoi1 = 0, vtoi2 = 0; //转换成int进行比较int m = version1.size(), n = version2.size();int i = 0, j = 0;while(i < m || j < n){int temp1 = 0, temp2 = 0; //存储这一段的值while(i < m && version1[i] != '.'){temp1 = temp1 * 10 + version1[i] - '0';i++;}i++;while(j < n && version2[j] != '.'){temp2 = temp2 * 10 + version2[j] - '0';j++;}j++;vtoi1 += temp1;vtoi2 += temp2;if(vtoi1 > vtoi2)return 1;else if(vtoi1 < vtoi2)return -1;}return 0;}
};
Accepted
81/81 cases passed (0 ms)
Your runtime beats 100 % of cpp submissions
Your memory usage beats 80.23 % of cpp submissions (6 MB)
这篇关于165. 比较版本号——字符串的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!