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

Posted 魔豆Magicbean

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Leetcode] 681. Next Closest Time 解题报告相关的知识,希望对你有一定的参考价值。

题目

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;
            
        
    
;

以上是关于[Leetcode] 681. Next Closest Time 解题报告的主要内容,如果未能解决你的问题,请参考以下文章

681. Next Closest Time(Accepted)

LeetCode笔记:Biweekly Contest 68

LeetCode笔记:Biweekly Contest 68

关于CLOS架构的举例 网络级 设备级 FATTREE网络 网络级CLOS 以及CLOS涉及的调度算法RR

LeetCode 31. Next Permutation

LeetCode - 31. Next Permutation