leetcode 539. Minimum Time Difference 时间最小差值 + C++中stringstream一个很好示例
2017-12-17 15:00
525 查看
Given a list of 24-hour clock time points in “Hour:Minutes” format, find the minimum minutes difference between any two time points in the list.
Example 1:
Input: [“23:59”,”00:00”]
Output: 1
Note:
The number of time points in the given list is at least 2 and won’t exceed 20000.
The input time is legal and ranges from 00:00 to 23:59.
题意很简单,注意这几个事项:
1)对string直接排序即可,字典排序是可以的
2)对于最后一个时间节点的计算需要加上24*60min
同样的本题注意对C++的stringstream的使用
建议和这一道题leetcode 537. Complex Number Multiplication C++中的stringstream真的很好用 一起学习
代码如下:
Example 1:
Input: [“23:59”,”00:00”]
Output: 1
Note:
The number of time points in the given list is at least 2 and won’t exceed 20000.
The input time is legal and ranges from 00:00 to 23:59.
题意很简单,注意这几个事项:
1)对string直接排序即可,字典排序是可以的
2)对于最后一个时间节点的计算需要加上24*60min
同样的本题注意对C++的stringstream的使用
建议和这一道题leetcode 537. Complex Number Multiplication C++中的stringstream真的很好用 一起学习
代码如下:
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> using namespace std; class Solution { public: int findMinDifference(vector<string>& timePoints) { int minDif = 24 * 60; sort(timePoints.begin(),timePoints.end()); for (int i = 0; i < timePoints.size(); i++) { string a = timePoints[i], b = timePoints[(i + 1) % timePoints.size()]; stringstream sa(a), sb(b); int h1, m1, h2, m2; char c; sa >> h1 >> c >> m1; sb >> h2 >> c >> m2; int dif = (h2 - h1) * 60 + (m2 - m1); if (i == timePoints.size() - 1) dif += 24 * 60; minDif = min(minDif, dif); } return minDif; } };
相关文章推荐
- leetcode 592. Fraction Addition and Subtraction 分数加法+C++stringstream的一个很好的示例
- 计算时间:一个C++运算符重载示例
- 实现一个栈(元素遵守先入后出顺序),能够通过 min 方法在 O(1)时间内获取栈中的最小元素。同时,栈的基本操作:入栈(Push)、出栈(Pop),也是在O(1)时间内完成的
- 阿里14笔试题-求一个二叉树最大值和最小值的差值绝对值
- 实现一个栈的pop,push,Min(最小值),时间复杂度为O(1)
- 一个很好很强大的js时间控件:显示年,月,日,时,分,秒
- 阿里14笔试题-求一个二叉树最大值和最小值的差值绝对值
- Android LocalService的一个示例 - 通过Binder从服务器获取时间
- leetcode 330. Patching Array 最小的添加/删除次数使sum为1到n + 一个很棒很值的做法
- CSS drop down的一个很好的示例
- 阿里14笔试题-求一个二叉树最大值和最小值的差值绝对值
- 两个时间之差,一个时间到当前的时间差 很好用
- 一个很好的js日历控件,支持键盘和时间选择
- LeetCode试题之““给定一个正整数A,找到最小的正整数B,它的每一个数字的乘法等于A。””
- datejs-一个很好用的时间日期JavaScript组件
- 阿里14笔试题-求一个二叉树最大值和最小值的差值绝对值
- leetcode 164. Maximum Gap 相邻元素的最大间隔 + 一个很好的桶排序示范
- leetcode 537. Complex Number Multiplication 复数乘法 + C++中stringstream很好示例
- 将一个数组划分为和差值最小的子数组
- 定义一个栈的数据结构,要求实现一个min函数,每次能够得到栈的最小值,并且要求Min的时间复杂度为O(1)