C++ AC 0ms simple solution


  • 0
    M
    class Solution {
    public:
        string nextClosestTime(string time) {
            vector<char> num{time[0],time[1],time[3],time[4]};
            sort(num.begin(),num.end());
    
            for (int i=4;i>=0;i--)
            {
                if (time[i]==':') continue;
                auto bound=upper_bound(num.begin(),num.end(),time[i]);
                if (bound!=num.end())
                {
                    time[i] = *bound;
                    if (isValid(time)) return time;
                }
                time[i] = num[0];
            }
            return time;
        }
    private:
        bool isValid(string t) {
            int hh=(t[0]-'0')*10+(t[1]-'0'), mm=(t[3]-'0')*10+(t[4]-'0');
            if (hh<0 || hh >=24 || mm<0 || mm >=60) return false;
            return true;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.