才一周没刷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. java基础-包

    浏览以下内容前,请点击并阅读 声明 为了使类型更容易查找和使用,避免命名冲突,以及可视范围的控制,程序员一般将相关的一些类型组合到一个包中.组合的类型包括类,接口,枚举和注释,枚举是一种特殊的类,而注 ...

  2. 【原】iOS学习之文件管理器(NSFileManager)和文件对接器(NSFileHandle)

    1.文件管理器(NSFileManager) 1> 主要作用及功能方法 主要作用:此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取. 功能方法: 2> 创建文件夹 创建所 ...

  3. PHP的排序算法跟查找算法

    排序算法: (1)冒泡排序 $arr = array(15,8,20,50,37,85,10,5,11,4); //冒泡排序 function maoPao($arr){ for($i = 0; $i ...

  4. 推荐Python Web开发测试驱动方法

    http://www.cnblogs.com/dkblog/archive/2013/06/14/3135914.html推荐 本人买的时候,京东打8.5折,现在降价啦,本书涵盖啦Django.Sel ...

  5. *HDU3047 并查集

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. centos关闭防火墙

    Centos7 关闭防火墙 CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下 1.直接关闭防火墙 systemctl stop firewalld.se ...

  7. Struts2拦截器的使用 (详解)

    Struts2拦截器的使用 (详解) 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈default ...

  8. C语言中static的作用

    (1)在函数体内,局部的static变量.生存周期为程序的整个生命周期:作用域却在定义了的函数体内.一个被声明为静态的变量在这个函数被调用过程中维持其值不变.因为它分配在静态存储区域,函数调用结束以后 ...

  9. 使用 PDO 方式将 Session 保存到 MySQL 数据中

    类: <?php /* 使用数据库保存session */ class DBHandler implements SessionHandlerInterface { protected $dbh ...

  10. swift-UserDefaults控制账号和密码

    import UIKit class FiveVC: UIViewController { //MARK:-------- 全局 常量 设置 let IsFirstLaunch = "IsF ...