【LeetCode】165. Compare Version Numbers
LeetCode version Numbers Compare
2023-09-11 14:20:27 时间
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.
Here is an example of version numbers ordering:
0.1 < 1.1 < 1.2 < 13.37
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
将被'.'分割字段逐个取出做比较即可。
class Solution { public: int compareVersion(string version1, string version2) { int len1 = version1.size(); int len2 = version2.size(); int begin1 = 0; int begin2 = 0; int end1 = 0; int end2 = 0; while(end1 != len1 && end2 != len2) { // extract section in version1 string sec1; while(end1 != len1 && version1[end1] != '.') end1 ++; if(end1 == len1) sec1 = version1.substr(begin1); else { sec1 = version1.substr(begin1, end1-begin1); begin1 = end1 + 1; end1 = begin1; } // extract section in version2 string sec2; while(end2 != len2 && version2[end2] != '.') end2 ++; if(end2 == len2) sec2 = version2.substr(begin2); else { sec2 = version2.substr(begin2, end2-begin2); begin2 = end2 + 1; end2 = begin2; } // compare sec1 and sec2 int num1 = atoi(sec1.c_str()); int num2 = atoi(sec2.c_str()); if(num1 > num2) return 1; if(num1 < num2) return -1; } if(end1 == len1 && end2 == len2) return 0; while(end1 != len1) {// version1 remains string sec1; while(end1 != len1 && version1[end1] != '.') end1 ++; if(end1 == len1) sec1 = version1.substr(begin1); else { sec1 = version1.substr(begin1, end1-begin1); begin1 = end1 + 1; end1 = begin1; } int num1 = atoi(sec1.c_str()); if(num1 > 0) return 1; } while(end2 != len2) {// version2 remains string sec2; while(end2 != len2 && version2[end2] != '.') end2 ++; if(end2 == len2) sec2 = version2.substr(begin2); else { sec2 = version2.substr(begin2, end2-begin2); begin2 = end2 + 1; end2 = begin2; } int num2 = atoi(sec2.c_str()); if(num2 > 0) return -1; } return 0; } };
相关文章
- There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:661) - skipping
- Java实现 LeetCode 749 隔离病毒(DFS嵌套)
- Java实现 LeetCode 600 不含连续1的非负整数(有些题为了避免使用位运算可以换成动态规划)...
- Java实现 LeetCode 445 两数相加 II
- Java实现 LeetCode 100 相同的树
- Java实现 LeetCode 46 全排列
- LeetCode-817. 链表组件【哈希表,链表,数组】
- leetcode 139. 单词拆分---完全背包问题之true or false类型
- leetcode 322. 零钱兑换----完全背包套路解法详细再探
- 【LeetCode Python实现】剑指 Offer II 038. 每日温度(中等)单调栈
- Leetcode 1624. 两个相同字符之间的最长子字符串(一次过)
- 【Mac系统】Vscode使用LeetCode插件报错‘leetcode.toggleLeetCodeCn‘ not found
- 【Leetcode刷题Python】611. 有效三角形的个数
- 【Leetcode刷题Python】222. 完全二叉树的节点个数
- 【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
- 【LeetCode】647.回文子串
- 【LeetCode】74.网络延迟时间
- 【LeetCode】两数之和II