Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- string nextClosestTime(string time) {
- vector<int> vi;
- for (char c : time) {
- if (c == ':') continue;
- vi.push_back(c - '0');
- }
- int hh = vi[0] * 10 + vi[1];
- int mm = vi[2] * 10 + vi[3];
- int original = hh * 60 + mm;
- sort(vi.begin(), vi.end());
- int ans = INT_MAX;
- set<int> candidates;
- 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) {
- int hh = vi[i] * 10 + vi[j];
- int mm = vi[k] * 10 + vi[l];
- if (hh >= 24 || mm >= 60) continue;
- int candidate = hh * 60 + mm;
- if (candidate <= 1440) {
- candidates.insert(candidate);
- }
- }
- }
- }
- }
- for (auto candidate : candidates) {
- if (candidate > original) {
- ans = min(ans, candidate);
- }
- }
- if (ans == INT_MAX) {
- ans = *(candidates.begin());
- }
- hh = ans / 60;
- mm = ans % 60;
- string str_hh = hh < 10 ? "0" + to_string(hh) : to_string(hh);
- string str_mm = mm < 10 ? "0" + to_string(mm) : to_string(mm);
- return str_hh + ":" + str_mm;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement