[Leetcode] 681. Next Closest Time 解题报告

题目要求根据给定的24小时制时间字符串,通过重新排列现有数字找到下一个最近的时间。解题思路是计算数字集合,设置四个指针分别对应小时和分钟的每一位,尝试递增分钟,判断是否溢出并调整小时。当小时或分钟溢出时,将其置为最小值并继续增加。示例展示了问题的解决方案和具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

Given a time represented in the format "HH:MM", form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.

You may assume the given input string is always valid. For example, "01:34", "12:09" are all valid. "1:34", "12:9" are all invalid.

Example 1:

Input: "19:34"
Output: "19:39"
Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later.  It is not 19:33, because this occurs 23 hours and 59 minutes later.

Example 2:

Input: "23:59"
Output: "22:22"
Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. It may be assumed that the returned time is next day's time since it is smaller than the input time numerically.

思路

首先计算出time中所包含的digits所形成的set,然后再计算出四个指针,分别指向hour的第一位和第二位,minute的第一位和第二位。接着看增加munite后是否会溢出(溢出有两种可能性,一种是超出分钟的最大范围59,另一种是指针超出了set的范围)。如果munite溢出了,则我们置munite置为最小,并且再增加hour。在增加hour的时候,如果发现hour也溢出了(hour溢出的可能性也有两种,一种是超出小时的最大范围23,另一种是指针超出了set的范围),则我们将hour也置为最小。

代码

class Solution {
public:
    string nextClosestTime(string time) {
        set<char> st;
        st.insert(time[0]), st.insert(time[1]), st.insert(time[3]), st.insert(time[4]);
        vector<int> vec;
        for (auto it = st.begin(); it != st.end(); ++it) {
            vec.push_back(*it - '0');
        }
        int h_first = distance(st.begin(), st.find(time[0]));
        int h_second = distance(st.begin(), st.find(time[1]));
        int m_first = distance(st.begin(), st.find(time[3]));
        int m_second = distance(st.begin(), st.find(time[4]));
        bool increase = false;
        getNextMinute(vec, m_first, m_second, increase);
        if (increase) {
            getNextHour(vec, h_first, h_second);
        }
        string ret;
        ret += vec[h_first] + '0';
        ret += vec[h_second] + '0';
        ret += ":";
        ret += vec[m_first] + '0';
        ret += vec[m_second] + '0';
        return ret;
    }
private:
    void getNextMinute(vector<int> &vec, int &m_first, int &m_second, bool &increase) {
        if (++m_second == vec.size()) {
            m_second = 0;
            if (++m_first == vec.size() || vec[m_first] >= 6) {     // overflow
                increase = true;
                m_first = 0;
            }
        }
    }
    void getNextHour(vector<int> &vec, int &h_first, int &h_second) {
        if (++h_second == vec.size()) {
            h_second = 0;
            if (++h_first == vec.size() || 10 * vec[h_first] + vec[h_second] >= 24) {
                h_first = 0;
            }
        }
        else {
            if (10 * vec[h_first] + vec[h_second] >= 24) {          // overflow
                h_first = 0;
                h_second = 0;
            }
        }
    }
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值