首页 > 资讯 > > 正文
天天观察:LeetCode 2224. Minimum Number of Operations to Convert Time
2023-05-18 13:02:00 哔哩哔哩

You are given two strings currentand correctrepresenting two 24-hour times.

24-hour times are formatted as "HH:MM", where HHis between 00and 23, and MMis between 00and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.

In one operation you can increase the time currentby 1515, or 60minutes. You can perform this operation any number of times.


(资料图片)

Return the minimum number of operations needed to convert currentto correct.

Example 1:

Input: current = "02:30", correct = "04:35"

Output: 3

Explanation:

We can convert current to correct in 3 operations as follows:- Add 60 minutes to current. current becomes "03:30".- Add 60 minutes to current. current becomes "04:30".- Add 5 minutes to current. current becomes "04:35".It can be proven that it is not possible to convert current to correct in fewer than 3 operations.

Example 2:

Input: current = "11:00", correct = "11:01"

Output: 1

Explanation: We only have to add one minute to current, so the minimum number of operations needed is 1.

Constraints:

currentand correctare in the format "HH:MM"

current <= correct

主要是写2个函数,一个是将小时转换成分钟,这样所有的数据都是用分钟去判断,

剩下就是判断调整的次数,用while循环即可;(我没看完题目,这里面current是一定小于correct的,但是代码里面是加了一层判断的)

下面是代码:

Runtime: 1 ms, faster than 94.12% of Java online submissions for Minimum Number of Operations to Convert Time.

Memory Usage: 40.9 MB, less than 61.03% of Java online submissions for Minimum Number of Operations to Convert Time.

x 广告