Saturday, September 23, 2017
681. Next Closest Time
bool isValid(string time) {
if(stoi(time.substr(0,2))>23) return false;
if(stoi(time.substr(3,2))>59) return false;
return true;
}
string nextClosestTime(string time) {
string t = time;
string s = {t[0], t[1], t[3], t[4]};
vector<string> v;
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
for(int k=0; k<4; k++)
for(int l=0; l<4; l++) {
t[0] = s[i], t[1] = s[j], t[3] = s[k], t[4] = s[l];
if(isValid(t)) v.push_back(t);
}
sort(v.begin(), v.end());
auto last = unique(v.begin(), v.end());
v.erase(last, v.end());
auto it = find(v.begin(), v.end(), time);
if(it+1 == v.end()) return *v.begin();
else return *(it+1);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment