1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| // @Title: 统计道路上的碰撞次数 (Count Collisions on a Road)
// @Author: 15816537946@163.com
// @Date: 2022-03-20 13:07:47
// @Runtime: 48 ms
// @Memory: 16 MB
class Solution {
public:
int countCollisions(string directions) {
int ans = 0;
bool flag = false;
for (char c : directions) {
if (c == 'R' || c == 'S') flag = true;
else if (flag) ans++;
}
flag = false;
for (int i = (int) directions.size() - 1; i >= 0; i--) {
char c = directions[i];
if (c == 'L' || c == 'S') flag = true;
else if (flag) ans++;
}
return ans;
}
};
|