才一周没刷leetcode,手就生了,这个题目不难,但是完全AC还是挺费劲的。

题目描述:

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

题目不难,就是比较字符串。但是有几点需要注意。

1.各大博客上po出的代码现在都无法AC,原因在于测试case修正了。比如一些算法直接将自连个子版本号分割后对齐转化为int型,现在有个case直接导致这里发生溢出。

2.可能有多个子版本号,这里要考虑全面。

代码如下:

class Solution {
public:
int compareVersion(string version1, string version2) { string v1_front, v1_back, v2_front, v2_back; vector<string> v1;
vector<string> v2; splitString(version1, v1);
splitString(version2, v2); vector<unsigned long long> v1_long;
vector<unsigned long long> v2_long; for (auto i = v1.begin(); i != v1.end(); i ++) {
if (i->size() != )
v1_long.push_back(stoull(*i));
else
v1_long.push_back(); } for (auto i = v2.begin(); i != v2.end(); i ++) {
if (i->size() != )
v2_long.push_back(stoull(*i));
else
v2_long.push_back(); } while (v1_long.size() < v2_long.size()) {
v1_long.push_back();
} while (v1_long.size() > v2_long.size()) {
v2_long.push_back();
} for (int i = ; i < v1_long.size(); i ++) {
if (v1_long[i] != v2_long[i]) {
return v1_long[i] > v2_long[i] ? : -;
}
}
return ; } void abandonFrontZeros(string & s)
{
int pos = ;
while (s[pos] == '' && pos < s.size()) {
++pos;
}
s = s.substr(pos, s.size() - pos);
} void splitString(string & s, vector<string> & v)
{
vector<int> dot_pos;
for (int i = ; i < s.size(); i ++) {
if (s[i] == '.') {
dot_pos.push_back(i);
}
} int b = ;
for (int i = ; i < dot_pos.size(); i ++) {
v.push_back(s.substr(b, dot_pos[i] - b));
b = dot_pos[i] + ;
} v.push_back(s.substr(b, s.size() - b)); for (auto i = v.begin(); i != v.end(); i ++) {
abandonFrontZeros(*i);
}
} };

感慨一些:C++11的一些新特性还是很方便的。string类的自带接口如substr()很方便。还有stoi(), stoull(), stod()等模板函数也很方便。

												

leetcode 165的更多相关文章

  1. [LeetCode] 165. Compare Version Numbers 比较版本数

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

  2. Java实现 LeetCode 165 比较版本号

    165. 比较版本号 比较两个版本号 version1 和 version2. 如果 version1 > version2 返回 1,如果 version1 < version2 返回 ...

  3. ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  4. Leetcode 165 Compare Version Numbers

    题意:比较版本号的大小 有点变态,容易犯错 本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号 class Solution { public: int c ...

  5. Java for LeetCode 165 Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  6. Java [Leetcode 165]Compare Version Numbers

    题目描述: Compare two version numbers version1 and version2.If version1 > version2 return 1, if versi ...

  7. [LeetCode] 278. First Bad Version 第一个坏版本

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...

  8. nomasp 博客导读:Android、UWP、Algorithm、Lisp(找工作中……

    Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸.本博客会持续更新.感谢您的支持.欢迎您的关注与留言.博客有多个专栏,各自是关于 Android应用开发 .Wi ...

  9. LeetCode 第 165 场周赛

    LeetCode 第 165 场周赛 5275. 找出井字棋的获胜者 5276. 不浪费原料的汉堡制作方案 5277. 统计全为 1 的正方形子矩阵 5278. 分割回文串 III C 暴力做的,只能 ...

随机推荐

  1. SSM框架整合首只拦路虎——Eclipse新建Maven Project界面select an archetype 空白

    首先给大家说,本篇博客没有技术价值,纯属个人学习总结,权当给大家添加一乐.事件如有雷同,纯属巧合,莫怪! 前一段时间一直在看<淘淘商城>这个教程,里面讲的是SSM框架的一个电商项目.这不是 ...

  2. jq点击显示,再点击隐藏

    每次都会遇到的问题: <script> $("button").click(function(){ if($(".div").css("d ...

  3. RobotFramework自动化测试之脚本编写(一)

    接触了上一篇的RF环境搭建及安装,相比大家都会觉得,哇塞,为什么要做这么多,那么复杂?装那么多干什么有什么用?写脚本会不会也很复杂? 其实首次安装的话 会觉得有点蒙,也不知道安装那么多是拿来干什么的, ...

  4. JS 生成GUID 方法

    var Guid={NewGuid: function () { var guid = (this._G() + this._G() +"-"+ this._G() +" ...

  5. mongoVUE的增删改查操作使用说明

    mongoVUE的增删改查操作使用说明 一. 查询 1. 精确查询 1)右键点击集合名,再左键点击Find 或者直接点击工具栏上的Find 2)查询界面,包括四个区域 {Find}区,查询条件格式{& ...

  6. About_类与对象

    所谓类,就是把具有相同行为,特征的归为一类: 1)什么是面向对象: 起初,“面向对象”是专指在程序设计中采用封装.继承.抽象等设计方法.可是,这个定义显然不能再适合现在情况.面向对象的思想已经涉及到软 ...

  7. log4j日志文件 log4j.xml log4j.properties配置

    1,导入log4j  jar包; 2,配置log4j.xml或log4j.properties文件; ------------------------------------------------- ...

  8. Jackson

    Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json.xml转换成Java对象. 1. 下载依赖库jar包 Jackson的jar all下载地址:http:// ...

  9. iOS开发之XCode模拟器不能连接网络

    新装的Xcode7 编译程序 出现 #warning: 获取app配置信息失败: The resource could not be loaded because the App Transport ...

  10. js无刷新上传文件

    传统的文件上传方式 <form action="" method="POST" enctype="multipart/form-data&quo ...