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 暴力做的,只能 ...
随机推荐
- MYSQL trigger 个人记录
同学要写个trigger,稍微帮他研究了下 以下主要是个人猜想理解的 主要讲update 我们在创建trigger时,数据库会对创建了trigger的表进行事件监听. 当表中的一条记录发生update ...
- 【BZOJ1503】[HAOI2007]反素数ant 搜索
结论题...网上讲的好的很多... #include <iostream> using namespace std; ]={,,,,,,,,,},num=; long long ans,n ...
- ZeroMQ接口函数之 :zmq_msg_init_data - 从一个指定的存储空间中初始化一个ZMQ消息对象的数据
ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq_msg_init_data zmq_msg_init_data(3) ØMQ Manual - ØMQ/3.2.5 ...
- oracle 数据库 时间差 年数、月数、天数、小时数、分钟数、秒数
declare l_start date := to_date('2015-04-29 01:02:03', 'yyyy-mm-dd hh24:mi:ss'); l_end date := to_da ...
- js-倒计时自动隐藏
<!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...
- Python之路第一课Day2--随堂笔记
入门知识拾遗 一.bytes类型 bytes转二进制然后转回来 msg="张杨" print(msg) print(msg.encode("utf-8")) p ...
- bzoj1724: [Usaco2006 Nov]Fence Repair 切割木板(贪心+堆)
一开始被题目读错题= =以为每次只能割一块,那么就是从大到小切 但是其实是可以分为几堆来切的 所以可以逆着来,变为合并n个木板代价最小 易证每次找最小的两堆合并代价最小 用优先队列维护堆..偷偷懒= ...
- Dom4jUtils.java
package com.vcredit.framework.utils; import org.apache.commons.lang3.StringUtils;import org.dom4j.Do ...
- H TC並沒有成為下一個摩托羅拉或諾基亞。
關於2014年第四季度,H T C在三季度財報說明中提到,“年度旗艦H T CO ne(M 8)與中端機型H T C D esire系列在競爭日趨激烈的智能手機市場保持穩定的銷售,市占率有所提升,延續 ...
- 第一章-第五题(你所在的学校有计算机科学专业和软件工程专业么?相关专业的教学计划和毕业出路有什么不同?阅读有关软件工程和计算机科学的区别的文章,谈谈你的看法。)--By 侯伟婷
我所在的本科学校和研究生学校都有计算机科学专业和软件工程专业.具体的教学计划无从得到,所以此情况无从对比,但是我从本科教务处网站找到了计算机科学专业和软件工程专业有关专业方面的课程,现列表如下. 表格 ...