Easy to understand C++


  • 0
    H
    class Solution {
    public:
        bool mp[10];
        bool judge(int hour,int mini){
            if(!mp[hour/10]||!mp[hour%10]||!mp[mini/10]||!mp[mini%10])return false;
            return true;
        }
        string nextClosestTime(string time) {
               char c1=time[0],c2=time[1],c3=time[3],c4=time[4];
               memset(mp,0,sizeof(mp));
               mp[c1-'0']=1;mp[c2-'0']=1;mp[c3-'0']=1;mp[c4-'0']=1;
               int hour=0,mini=0;
               hour+=(c1-'0')*10+c2-'0';
               mini+=(c3-'0')*10+c4-'0';
               int carry=0;
               for(int i=1;;i++){
                   mini++;
                   carry=mini/60;
                   mini%=60;
                   hour+=carry;
                   hour%=24;
                   if(judge(hour,mini)){break;}
               }
               string ans="";
               ans+=(hour/10)+'0';ans+=(hour%10)+'0';ans+=":";
               ans+=(mini/10)+'0';ans+=(mini%10)+'0';
            return ans;
        }
    };
    

Log in to reply
 

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