リートコード681。次に近い時間



Leetcode 681 Next Closest Time



'HH:MM'の形式で表される時刻を指定して、現在の数字を再利用して次に近い時刻を作成します。 1桁を再利用できる回数に制限はありません。

指定された入力文字列は常に有効であると想定できます。たとえば、「01:34」、「12:09」はすべて有効です。 「1:34」、「12:9」はすべて無効です。



Example 1: Input: '19:34' Output: '19:39' Explanation: The next closest time choosing from digits 1, 9, 3, 4, is 19:39, which occurs 5 minutes later. It is not 19:33, because this occurs 23 hours and 59 minutes later. Example 2: Input: '23:59' Output: '22:22' Explanation: The next closest time choosing from digits 2, 3, 5, 9, is 22:22. It may be assumed that the returned time is next day's tim

影響を受ける:
各桁の現在の時刻で新しい時刻を作成し、新しい最小時刻と元の時差を作成します。この新しい時間の出力
アイデア:
時間の中央値が小さいため、それぞれを列挙して新しい時間に結合し、新しい時間の時間を元の2番目の比較にロードすることができます。時間の形式、つまり25:91時間のクラスはありそうもないことに注意してください。

class Solution { public: int get(string& s, int i, int j, int k, int p) { int x = (s[i] - ')') * 10 + s[j] - '0' int y = (s[k] - '0') * 10 + s[p] - '0' int sec = x * 3600 + y * 60 return sec } string nextClosestTime(string time) { string s = '' for (int i = 0 i <5 ++i) if(time[i] != ':') s += time[i] int be = get(s, 0, 1, 2, 3) int ans = 100000000 string t = '' for (int i = 0 i < 4 ++i) { for (int j = 0 j < 4 ++j) { for (int k = 0 k < 4 ++k) { for (int p = 0 p <4 ++p) { //if (i == 0 && j == 1 && k == 2 && p == 3) continue if (s[k]>= '6') continue if (s[i] >= '3') continue if (s[i] == '2' && s[j] > '4') continue if (s[i] == '2' && s[j] == '4') int as = get(s, i, j, k, p) if (as == be) continue if (as

コードを少し長く書いてください、アイデアは十分に単純ですああ