才一周没刷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. getFields()和getDeclaredFields()的区别

    getFields()获得某个类的所有的公共(public)的字段,包括父类. getDeclaredFields()获得某个类的所有申明的字段,即包括public.private和proteced, ...

  2. 全文检索原理以及es

    最近要做个文章搜索,对全文检索原理以及es原理进行了一些调研, 1.  es索引文件为多个文本文件描述,索引文件中的内容构成可见 http://elasticsearch.cn/article/86 ...

  3. sql 动态行转列

    create table u01 (医案编号 varchar(5),药物编号 varchar(5)) insert into u01 select '01','01' union all select ...

  4. 第八章 springboot + mybatis + 多数据源

    http://www.cnblogs.com/java-zhao/p/5413845.html

  5. 畅通工程[HDU1863]

    畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...

  6. js的三种继承方式及其优缺点

    [转] 第一种,prototype的方式: //父类 function person(){ this.hair = 'black'; this.eye = 'black'; this.skin = ' ...

  7. iOS PCH文件

    在Xcode6之前,创建一个新的工程,Xcode会再Support Files文件夹下自动创建一个"工程名 - prefix.pch"文件,也是一个头文件,pch文件的内容能被项目 ...

  8. Zxing库

    一.介绍 Zxing是一个开放的源码,用java实现的多种样式的1D/2D条码处理库,它包含了联系到其他语言的端口.Zxing可以实现手机的内置摄像头完成条码的扫描及解码.目前支持:UPC-A ,UP ...

  9. php进程占用大量cpu优化

    使用TOP 命令发现php进程占用大量的cpu,达到100%,需要优化. 1 ll /proc/6264/fd 查看进程正在使用的资源 2 strace -p 6264 追踪进程正在做的事情 引用 h ...

  10. php取整函数ceil,floor,round,intval函数的区别

    开发过程中,遇到数据处理取整的时候,你会用哪个呢,小涛来介绍一下:PHP取整函数有ceil,floor,round,intval,下面详细介绍一下: 1.ceil — 进一法取整说明float cei ...