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. C++学习001-注释

    天了噜,感觉自己最近好堕落啊,  在等待项目任务书到来的时候,在来好好学习学习C++ 今天来学习一下C++的注释风格 编写环境 Qt 5.7 1. //注释 // ui->setupUi(thi ...

  2. 在Kotlin上怎样用Mockito2 mock final 类(KAD 23)

    作者:Antonio Leiva 时间:Mar 2, 2017 原文链接:https://antonioleiva.com/mockito-2-kotlin/ 如我们在前面文章中谈到的,Kotlin最 ...

  3. Ajax跨域请求解决方式

    前端 jQuery方式 .ajax({ type: "POST", url: "http://xxx.com/api/test", dataType: 'jso ...

  4. [GraphSAGE] docker安装与程序运行

    安装Docker与程序运行 1. requirements.txt Problem: Downloading https://files.pythonhosted.org/packages/69/cb ...

  5. Django,Celery, rabbitmq

    学习Django 2 by Example书中的456页,运行 celery -A myshop worker -l info 报错.虽然特别指定了Celery的版本,也没用.之前使用的是标准安装:下 ...

  6. Android 之Buletooth

    一:概要: Android提供了Buletooth的API ,通过API 我们可以进行如下的一些操作: 1.扫描其他的蓝牙设备 2.查询能配对的蓝牙设备 3.建立RFCOMM 通道 4.连接其他的蓝牙 ...

  7. skip-grant-tables 修改linux的mysql忘记root密码

    skip-grant-tables 修改linux的mysql忘记root密码 今天修改mysql中的admin用户权限,在执行update user set host =' %' where use ...

  8. 基于eth快速发行自己的数字货币

    我们总在寻觅,也不断迷失. 像一颗飘摇的韭菜,彷徨而又无奈,无奈又彷徨. 如果你问我未来,我不知去向何方 我只知道生长,恣意野蛮. 我们不断在追寻一款爆款的项目,一个百倍币千倍币,却没有想到,实际上做 ...

  9. MySQL日常管理

    DB2最佳分页语句 SELECT * FROM ( SELECT inner2_.*, ROWNUMBER() OVER(ORDER BY ORDER OF inner2_) AS rownumber ...

  10. this.getClass().getResource()示例详解

    public class ResourceTest extends TimerTask{ @Override    public void run() {        System.out.prin ...