Minimum Time Difference 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/minimum-time-difference/description/


Description

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:

  1. The number of time points in the given list is at least 2 and won't exceed 20000.
  2. The input time is legal and ranges from 00:00 to 23:59.

Solution

class Solution {
public:
void timeConvert(vector<int>& times, vector<string>& timePoints) {
int size = timePoints.size();
for (int i = 0; i < size; i++) {
times[i] = ((timePoints[i][0] - '0') * 10 +
(timePoints[i][1] - '0')) * 60 +
(timePoints[i][3] - '0') * 10 +
(timePoints[i][4] - '0');
}
} int getDiff(int a, int b) {
int t = b - a;
if (t > 720)
return 1440 - t;
else
return t;
} int findMinDifference(vector<string>& timePoints) {
if (timePoints.empty() || timePoints.size() == 1)
return 0;
int size = timePoints.size();
vector<int> times(size);
timeConvert(times, timePoints);
sort(times.begin(), times.end());
int mind = getDiff(times[0], times[1]);
int t;
for (int i = 0; i <= size - 2; i++) {
t = getDiff(times[i], times[i + 1]);
if (t < mind)
mind = t;
}
t = getDiff(times[0], times[size - 1]);
if (t < mind)
mind = t;
return mind;
}
};

解题描述

这道题考察的主要是字符串的比较处理,相对简单,主要问题在于时间字符串与时间数值之间的转换。

[Leetcode Week10]Minimum Time Difference的更多相关文章

  1. LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  2. 51. leetcode 530. Minimum Absolute Difference in BST

    530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...

  3. [LeetCode] Minimum Time Difference 最短时间差

    Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minut ...

  4. [LeetCode] Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  5. LeetCode Minimum Time Difference

    原题链接在这里:https://leetcode.com/problems/minimum-time-difference/description/ 题目: Given a list of 24-ho ...

  6. LeetCode Minimum Absolute Difference in BST

    原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a b ...

  7. 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...

  8. 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  9. 【LeetCode】539. Minimum Time Difference 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/minimum-t ...

随机推荐

  1. Python 3基础教程29-os模块

    本文介绍os模块,主要是介绍一些文件的相关操作. 你还有其他方法去查看os 1. help() 然后输入os 2. Python接口文档,前面提到的用浏览器打开的,os文件路径为:C:\Users\A ...

  2. 《python核心编程第二版》第1章练习

    1–1. 安装 Python.请检查 Python 是否已经安装到你的系统上,如果没有,请下载并 安装它 略 1–2.  执行 Python.有多少种运行 Python 的不同方法?你喜欢哪一种?为什 ...

  3. C计算了一下

    #include <stdio.h> int main(){ int a,b,c,e; a=6 + 5 / 4 - 2; b=2 + 2 * (2 * 2 - 2) % 2 / 3; c= ...

  4. WebSocket 与 HTTP/2

    个人笔记 WebSocket WebSocket 是一个双向通信协议,它在握手阶段采用 HTTP/1.1 协议(暂时不支持 HTTP/2). 握手过程如下: 首先客户端向服务端发起一个特殊的 HTTP ...

  5. php+Mysql 页面登录代码

    登录界面设置: <?php/** * Created by xx. * User: msi * Date: 2017/10/26 * Time: 18:12 *///session每次用之前都要 ...

  6. wutianqi 博客 母函数

    母函数(Generating function)详解 — Tanky Woo 在数学中,某个序列的母函数(Generating function,又称生成函数)是一种形式幂级数,其每一项的系数可以提供 ...

  7. Nginx代理实现跨域

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  8. 在浏览器中从FTP下载文件

    public static class FTPHelper { /// <summary> /// 得到特定FTP目录的文件列表 /// </summary> /// < ...

  9. 在 Linux 安装 JDK 和 tomcat(菜鸡级别)

    安装JDK 卸载 OPENJDK rpm -qa|grep jdk  // 查看当前的jdk情况 yum -y remove java java-1.7.0-openjdk* // 卸载openjdk ...

  10. iOS进阶--提高XCode编译速度、Xcode卡顿解决方案

    提升编译链接的速度主要有以下三个方式: 1. 提高XCode编译时使用的线程数 defaults write com.apple.Xcode PBXNumberOfParallelBuildSubta ...