c++ beats 84.01%


  • 0
    E

    This runtime is 0ms ?!

    string nextClosestTime(string time) {
        set<char>hash;//store unique digits orderly.
        for(char c:time){if(c==':')continue;hash.insert(c);}
        int i=4;
    //from back to front, if find a bigger and suitable digit, replace it, and replace all the back with the smallest digit
        for(;i>=0;i--){
            if(time[i]==':')continue;
            auto it=hash.upper_bound(time[i]);//binary search
            if(it!=hash.end()&&(\
                (i==4&&time[i]<='9')||\
                (i==3&&*it<='5')||\
                (i==1&&((time[0]<'2'&&time[i]<'9')||*it<='3'))||\
                (i==0&&(*it<=1||(*it==2&&time[1]<='3')))\
                )){
                time[i]=*it;i++;break;
            }
        }
        for(;i<=4;i++){
            if(time[i]!=':')time[i]=*hash.begin();
        }
        return time;
    }
    

Log in to reply
 

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