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 };
时间: 2024-10-31 07:22:14

leetcode-Compare Version Numbers-165的相关文章

[LeetCode] Compare Version Numbers

Question: 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 

LeetCode—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 . charac

[LeetCode] 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 . characte

No.165 Compare Version Numbers

No.165 Compare Version NumbersCompare 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

leetCode 165. Compare Version Numbers 字符串

165. 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 t

165. Compare Version Numbers - LeetCode

Question 165. Compare Version Numbers Solution 题目大意: 比较版本号大小 思路: 根据逗号将版本号字符串转成数组,再比较每个数的大小 Java实现: public int compareVersion(String version1, String version2) { String[] v1Arr = version1.split("\\."); String[] v2Arr = version2.split("\\.&qu

LeetCode:Compare Version Numbers - 比较版本号

1.题目名称 Compare Version Numbers(比较版本号) 2.题目地址 https://leetcode.com/problems/compare-version-numbers/ 3.题目内容 英文:Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. 中

【leetcode 字符串处理】Compare Version Numbers

[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume

【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 . 

leetcode 152: Compare Version Numbers

Compare Version Numbers Total Accepted: 2468 Total Submissions: 17382 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 a