Re: [閒聊] 每日leetcode

作者: oin1104 (是oin的說)   2024-09-16 15:42:12
題目
給你一堆時間
找最小的時間差距
思路
記錄後sort
遍歷一次
比對相鄰時間之間的差距
回傳最小的
```cpp
class Solution {
public:
int htom(string k)
{
int res = stoi(k.substr(3,2));
res += stoi(k.substr(0,2)) * 60;
return res;
}
int findMinDifference(vector<string>& timePoints)
{
vector<int> save;
int n = timePoints.size();
for(string k : timePoints)
{
save.push_back(htom(k));
}
sort(save.begin() , save.end());
int res = abs(save[n-1] - 1440 - save[0] );
for(int i = 1 ; i < n ; i ++)
{
res = min(res , abs(save[i] - save[i-1]));
}
return res;
}
};
```

Links booklink

Contact Us: admin [ a t ] ucptt.com