681. Next Closest Time
我的思路:
这条题目只需模拟一下就好了
我的代码:
写的丑求轻喷
class Solution {
public:
string nextClosestTime(string time) {
set<char> tb;
int len = time.size();
for(int i = 0; i < len; i++) {
tb.insert(time[i]);
}
string hr_str = time.substr(0,2);
string min_str = time.substr(3,2);
int hr = stoi(hr_str);
int minute = stoi(min_str);
while(true) {
minute++;
if(minute > 59) {
hr++;
minute = 0;
if(hr > 23) {
hr = 0;
}
}
if(minute < 10) {
min_str = "0" + itos(minute);
} else {
min_str = itos(minute);
}
if(hr < 10) {
hr_str = "0" + itos(hr);
} else {
hr_str = itos(hr);
}
string res = hr_str + ":" + min_str;
bool valid = true;
for(int i = 0; i < 5; i++) {
if(!tb.count(res[i])) {
valid = false;
break;
}
}
if(valid) {
return res;
}
}
return "";
}
string itos(int a) {
stringstream ss;
ss << a;
return ss.str();
}
int stoi(string s) {
int result;
stringstream ss;
ss << s;
ss >> result;
return result;
}
};