We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fa116d7 commit 57feb4dCopy full SHA for 57feb4d
Compare Version Numbers/Compare Version Numbers.cpp
@@ -0,0 +1,22 @@
1
+/*
2
+ 比较版本号的大小
3
+ 从左到右依次比较被'.'分割成的区域,如果大小不等就直接返回结果,大小相同再比较下一个
4
+ 每个区域的字符串化成数字进行比较
5
+*/
6
+
7
+class Solution {
8
+public:
9
+ int compareVersion(string version1, string version2) {
10
+ int i = 0,j = 0;
11
+ while(i < version1.size() || j < version2.size()){
12
+ int num1 = 0,num2 = 0;
13
+ while(i < version1.size() && version1[i] != '.') num1 = num1 * 10 + version1[i++] - '0';
14
+ while(j < version2.size() && version2[j] != '.') num2 = num2 * 10 + version2[j++] - '0';
15
+ i++;
16
+ j++;
17
+ if(num1 > num2) return 1;
18
+ if(num1 < num2) return -1;
19
+ }
20
+ return 0;
21
22
+};
0 commit comments