码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Compare Version Numbers

时间:2015-03-19 06:17:36      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

Two notes:

1. I dont know whether C++ has a good split function for STL as the JAVA. Need to figure it out.

2. At the beginning, I tried to set tmp1 = tmp2 = INT_MIN. But one of test case does not work "1.0" and "1". So those two does not need to set be INT_MIN or INT_MAX every time. It should be 0 at this time.

 1 class Solution {
 2 public:
 3     vector<string> splits(string s) {
 4         vector<string> result;
 5         int len = s.size();
 6         for (int i = len-1; i >= 0; i--) {
 7             if (s[i] == .) {
 8                 len = i;
 9             } else if (i == 0 || s[i-1] == .) {
10                 result.push_back(s.substr(i, len - i));
11             }
12         }
13         return result;
14     }
15     int compareVersion(string version1, string version2) {
16         if (version1.size() == 0 && version2.size() == 0) return 0;
17         if (version1.size() == 0) return -1;
18         if (version2.size() == 0) return 1;
19         vector<string> list1 = splits(version1);
20         vector<string> list2 = splits(version2);
21         int l1 = list1.size()-1, l2 = list2.size()-1;
22         while (l1 >= 0 || l2 >= 0) {
23             int tmp1 = 0, tmp2 = 0;
24             if (l1 >= 0) {
25                 tmp1 = stoi(list1[l1--]);
26             }
27             if (l2 >= 0) {
28                 tmp2 = stoi(list2[l2--]);
29             }
30             if (tmp1 > tmp2) return 1;
31             else if (tmp1 < tmp2) return -1;
32         }
33         return 0;
34     }
35 };

 

LeetCode – Refresh – Compare Version Numbers

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4349274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!