【leetcode】Compare Version Numbers

Compare Version Numbers

Compare two version numbers version1 and version1.
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.

 
 
考虑以下几种情况即可:

Example1: version1=="11.22.33", version2=="11.22.22". 11 == 11; 22 == 22; 33 > 22; return 1.

Example2: version1=="11.22.33", version2=="11.22.33". 11 == 11; 22 == 22; 33 == 33; return 0.

Example3: version1=="11.22.33", version2=="11.22.33.00.00". 11 == 11; 22 == 22; 33 == 33; remaining version2 equals to 0; return 0.

Example4: version1=="11.22.33.00.01", version2=="11.22.33". 11 == 11; 22 == 22; 33 == 33; remaining version1 contains 01; return 1.

 
 
第一种方法,把字符串拆分到vector中,然后进行对比:
 1 class Solution {
 2 public:
 3  
 4     
 5     void split(std::string& s, std::string& delim,std::vector< std::string >& ret)
 6     {
 7      size_t last = 0;
 8         size_t index=s.find_first_of(delim,last);
 9      while (index!=std::string::npos)
10      {
11       ret.push_back(s.substr(last,index-last));
12       last=index+1;
13       index=s.find_first_of(delim,last);
14      }
15      if (index-last>0)
16      {
17       ret.push_back(s.substr(last,index-last));
18      }
19     }
20    
21     int compareVersion(string version1, string version2) {
22         
23         //拆分到vector中
24         vector<string> v1,v2;
25         string delim=".";
26        
27         split(version1,delim,v1);
28         split(version2,delim,v2);
29        
30         int n=v1.size()>v2.size()?v1.size():v2.size();
31        
32        
33         for(int i=0;i<n;i++)
34         {
35  
36             //当v1完了,只需要看v2末尾是否都为0即可
37             if(i>=v1.size())
38             {
39                 if(atoi(v2[i].c_str())!=0)
40                 {
41                     return -1;
42                 }
43             }
44            
45             //当v2完了,只需要看v1末尾是否都为0即可
46             if(i>=v2.size())
47             {
48                 if(atoi(v1[i].c_str())!=0)
49                 {
50                     return 1;
51                 }
52             }
53            
54             //v1,v2没完,则比较大小即可
55             if(i<v1.size()&&i<v2.size())
56             {
57                 if(atoi(v1[i].c_str())>atoi(v2[i].c_str()))
58                 {
59                     return 1;
60                 }
61                 if(atoi(v1[i].c_str())<atoi(v2[i].c_str()))
62                 {
63                     return -1;
64                 }
65             }
66         }
67         return 0;
68     }
69 };

 

 
第二种解法,采用库函数
C++ 11中,stoi可以从字符串string中取得数字,并返回不是数字下一个位置。
string中的erase函数:string& erase (size_t pos = 0, size_t len = npos)
 
 
 1 class Solution {
 2 public:
 3  
 4     int compareVersion(string version1, string version2) {
 5  
 6  
 7         while(!(version1.empty()&&version2.empty()))
 8         {
 9             size_t pos1=0;
10             size_t pos2=0;
11             int n1,n2;
12  
13             n1=version1.empty()?0:stoi(version1,&pos1);
14             n2=version2.empty()?0:stoi(version2,&pos2);
15  
16            
17             if(n1>n2)
18                 return 1;
19             else if(n1<n2)
20                 return -1;
21            
22            
23             if(!version1.empty())
24                 version1.erase(0,pos1+1);
25             if(!version2.empty())
26                 version2.erase(0,pos2+1);
27         }
28        
29         return 0;
30        
31     }
32 };

 

 


 
posted @ 2015-01-26 23:19  H5开发技术  阅读(216)  评论(0编辑  收藏  举报