【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.
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 };
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 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现