leetcode 165
才一周没刷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的更多相关文章
- [LeetCode] 165. Compare Version Numbers 比较版本数
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- Java实现 LeetCode 165 比较版本号
165. 比较版本号 比较两个版本号 version1 和 version2. 如果 version1 > version2 返回 1,如果 version1 < version2 返回 ...
- ✡ leetcode 165. Compare Version Numbers 比较两个字符串数字的大小 --------- java
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Leetcode 165 Compare Version Numbers
题意:比较版本号的大小 有点变态,容易犯错 本质是字符串的比较,请注意他的版本号的小数点不知1个,有的会出现01.0.01这样的变态版本号 class Solution { public: int c ...
- Java for LeetCode 165 Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
- Java [Leetcode 165]Compare Version Numbers
题目描述: Compare two version numbers version1 and version2.If version1 > version2 return 1, if versi ...
- [LeetCode] 278. First Bad Version 第一个坏版本
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- nomasp 博客导读:Android、UWP、Algorithm、Lisp(找工作中……
Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸.本博客会持续更新.感谢您的支持.欢迎您的关注与留言.博客有多个专栏,各自是关于 Android应用开发 .Wi ...
- LeetCode 第 165 场周赛
LeetCode 第 165 场周赛 5275. 找出井字棋的获胜者 5276. 不浪费原料的汉堡制作方案 5277. 统计全为 1 的正方形子矩阵 5278. 分割回文串 III C 暴力做的,只能 ...
随机推荐
- php中session原理及安全性问题
有一点我们必须承认,大多数web应用程序都离不开session的使用.这篇文章将会结合php以及http协议来分析如何建立一个安全的会话管理机制 我们先简单的了解一些http的知识,从而理解该协议 ...
- NOIP2008 ISBN号码(一桶水)【A005】
[A005]NOIP2008 ISBN号码(一大桶水)[难度A]———————————————————————————————————————————————————————————————————— ...
- border
1.border-width:不支持百分比值 支持下面属性值: thin:1px medium:3px thick:5px 2.border-style类型 solid:实线 dashed:虚线(方形 ...
- Leetcode Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [BZOJ4029][HEOI2015] 定价
Description 在市场上有很多商品的定价类似于 999 元.4999 元.8999 元这样.它们和 1000 元.5000 元和 9000 元并没有什么本质区别,但是在心理学上会让人感觉便宜很 ...
- Java_ClassLoader内存溢出-从tomcat的reload说起
原文链接:http://nius.me/classloader-memory-leak/ 对于j2ee项目,一直使用eclipse的wtp,每次修改代码后,可以自动热部署.简单的项目wtp似乎没什么问 ...
- 静态属性,直接把iis搞垮掉 Http error 503 Service Unavailable
属性有个好处,可以在get的时候做一些特殊处理,比如返回一个默认值,正是这个特性,吸引我讲静态字段修改了成静态属性,代码如下: public static string 微信订阅号 { get { i ...
- ADT Ubuntu X64 下ia32-libs替换等【待编辑】
sudo apt-get install ia32-libs apt-get install libglib2.0-0:i386 libpng12-0:i386 libsm6:i386 libxren ...
- EasyUI配置和组件
首先在webcontent添加配置文件 新建静态或动态网站,在title的下面加入五个配置文件路径,注意:循序不能乱 <!-- 顺序不可以乱 --> <!-- 1.jQuery的js ...
- VS2013 配置pthread
参考:http://blog.csdn.net/qianchenglenger/article/details/16907821 一.下载地址 ftp://sourceware.org/pub/pth ...