[leetcode] 165. Compare Version Numbers 解题报告
2015-12-29 15:55
525 查看
题目链接:https://leetcode.com/problems/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 the
The
For instance,
second-level revision of the second first-level revision.
Here is an example of version numbers ordering:
思路:一个比较明确的思路是,将字符串以‘.’分割成整数,然后从前往后比较,一旦发现不一样的就说明可以判定大小了。
代码如下:
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
.character does not represent a decimal point and is used to separate number sequences.
For instance,
2.5is 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
思路:一个比较明确的思路是,将字符串以‘.’分割成整数,然后从前往后比较,一旦发现不一样的就说明可以判定大小了。
代码如下:
class Solution { public: void divid(string str, vector<int>& vec) { string tem; for(int i = 0; i< str.size(); i++) { if(str[i] != '.') tem.append(1, str[i]); else { vec.push_back(stoi(tem)); tem = ""; } } } int compareVersion(string version1, string version2) { vector<int> nums1, nums2; version1 += ".", version2 += "."; divid(version1, nums1); divid(version2, nums2); int m =0, n =0; for(int i = 0; i < max(nums1.size(), nums2.size()); i++) { int val1 = 0, val2 = 0; if(i < nums1.size()) val1 = nums1[i]; if(i < nums2.size()) val2 = nums2[i]; if(val1 > val2) return 1; if(val1 < val2) return -1; } return 0; } };
相关文章推荐
- 报错active arch=arm64, VALID_ARCHS=armv7 armv7s
- Linux I/O优化 磁盘读写参数设置
- Diagramming for WinForms 的安装和配置
- MYSQL-使用mysqldump创建数据库快照
- linux 下,解决tomcat服务器接收中文乱码的问题
- JAVA中的集合
- 一、黑客挚友: ctypes
- 跨域问题
- Hdu 2076
- Git SSH Key 生成步骤
- 2、VC 设置EXCEL单元格的格式
- SVG脚本编程简介
- iOS https 证书信任漏洞解决办法
- MySQL变量
- 网站 服务器要求要求用户提供用户名与密码
- 怎么使用 ab.exe 测试多个url。 how to use ab.exe test many url
- jquery实现树形菜单完整代码
- maven项目 误点Disable Maven Nature 变普通 java web项目
- 彻底理解thunk函数与co框架
- Java 数据库操作