Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 374ff72

Browse files
committed
Add C++ solution for leetcode 1904.
1 parent 6ef00cc commit 374ff72

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<vector>
2+
#include<algorithm>
3+
#include<iostream>
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
7+
class Solution {
8+
public:
9+
int numberOfRounds(string startTime, string finishTime) {
10+
int startMinu = getMinutes(startTime);
11+
int endMinu = getMinutes(finishTime);
12+
if (endMinu < startMinu) endMinu += 24 * 60; /* case: ["20:00", "06:00"], 包括了 00:00 */
13+
else if (endMinu - startMinu < 15) return 0; /* case: ["00:47", "00:57"] */
14+
int validStart = startMinu % 15 != 0 ? (startMinu / 15 + 1) * 15 : startMinu / 15 * 15;
15+
int validEnd = endMinu / 15 * 15;
16+
return (validEnd - validStart) / 15; /* 有效区间: 原区间的子区间, 两端均收缩, 即endTime/15向下取整 - startTime/15向上取整 */
17+
}
18+
int getMinutes(string ts)
19+
{
20+
int hour = stoi(ts.substr(0, 2));
21+
int minute = stoi(ts.substr(3, 2));
22+
return 60*hour + minute;
23+
}
24+
};
25+
26+
// Test
27+
int main()
28+
{
29+
Solution sol;
30+
string start = "20:00";
31+
string end = "06:00";
32+
auto res = sol.numberOfRounds(start, end);
33+
cout << res << endl;
34+
35+
return 0;
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /