首页 > 代码库 > leetcode-Compare Version Numbers-165

leetcode-Compare Version Numbers-165

输入两个字符串,代表两个版本,比较大小

注意:

1.小数点个数不一定,如a=1.0.1,b=1

2.1.0==1

 1 class Solution { 2 public: 3      void func(vector<int> &a,string stt,int len){ 4         string s; 5         int i; 6         for(i=0;i<len;i++){ 7             if(stt[i]!=.) s+=stt[i]; 8             else{ 9                 istringstream st;10                 st.str(s);11                 int tmp;12                 st>>tmp;13                 a.push_back(tmp);14                 s.clear();15             }16         }17         istringstream st;18         st.str(s);19         int tmp;20         st>>tmp;21         a.push_back(tmp);22     }23     int compareVersion(string version1, string version2) {24         int m=version1.size();25         int n=version2.size();26         vector<int> a;27         vector<int> b;28         func(a,version1,m);29         func(b,version2,n);30         int i=0;31         int mi=min(a.size(),b.size());32         while(i<mi){33             if(a[i]<b[i]) return -1;34             if(a[i]>b[i]) return 1;35             i++;36         }37         if(i==a.size()&&i<b.size()){38             while(i<b.size()){39                 if(b[i]!=0) return -1;40                 i++;41             }42             return 0;43         }44         if(i<a.size()&&i==b.size()){45             while(i<a.size()){46                 if(a[i]!=0) return 1;47                 i++;48             }49             return 0;50         }51         return 0; 52     }53 };

 

leetcode-Compare Version Numbers-165