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 4228f65

Browse files
committed
feat: add solutions to lc problem: No.2211
No.2211.Count Collisions on a Road
1 parent 8707e85 commit 4228f65

File tree

9 files changed

+146
-64
lines changed

9 files changed

+146
-64
lines changed

‎solution/2200-2299/2211.Count Collisions on a Road/README.md‎

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61-
这个题比较有意思,最后规律是左边向左的车辆可以出去,右边向右的车辆可以出去,中间不是 S 的都出不来。
61+
- 去除前缀为 `L` 的字符;
62+
- 去除后缀为 `R` 的字符;
63+
- 剩余的字符串中,除了 `S` 以外的字符,都会贡献一次碰撞次数。
6264

6365
<!-- tabs:start -->
6466

@@ -69,15 +71,8 @@
6971
```python
7072
class Solution:
7173
def countCollisions(self, directions: str) -> int:
72-
l, r = 0, len(directions)-1
73-
while l <= r and directions[l] == 'L':
74-
l += 1
75-
while l <= r and directions[r] == 'R':
76-
r -= 1
77-
count = 0
78-
for i in range(l, r+1):
79-
count += directions[i] != 'S'
80-
return count
74+
d = directions.lstrip('L').rstrip('R')
75+
return len(d) - d.count('S')
8176
```
8277

8378
### **Java**
@@ -87,25 +82,48 @@ class Solution:
8782
```java
8883
class Solution {
8984
public int countCollisions(String directions) {
90-
int l = 0, r = directions.length() - 1, count = 0;
91-
while (l <= r && directions.substring(l, l + 1).equals("L")) {
92-
l++;
85+
char[] ds = directions.toCharArray();
86+
int n = ds.length;
87+
int l = 0;
88+
int r = n - 1;
89+
while (l < n && ds[l] == 'L') {
90+
++l;
9391
}
94-
while (l <= r && directions.substring(r, r +1).equals("R")) {
95-
r--;
92+
while (r >=0 && ds[r] =='R') {
93+
--r;
9694
}
97-
for (int i = l; i <= r; i++) {
98-
if (!directions.substring(i, i + 1).equals("S")) count += 1;
95+
int ans = 0;
96+
for (int i = l; i <= r; ++i) {
97+
if (ds[i] != 'S') {
98+
++ans;
99+
}
99100
}
100-
return count;
101+
return ans;
101102
}
102103
}
103104
```
104105

105106
### **TypeScript**
106107

107108
```ts
108-
109+
function countCollisions(directions: string): number {
110+
const n = directions.length;
111+
let l = 0,
112+
r = n - 1;
113+
while (l < n && directions[l] == 'L') {
114+
++l;
115+
}
116+
while (r >= 0 && directions[r] == 'R') {
117+
--r;
118+
}
119+
let ans = 0;
120+
for (let i = l; i <= r; ++i) {
121+
if (directions[i] != 'S') {
122+
++ans;
123+
}
124+
}
125+
return ans;
126+
}
109127
```
110128

111129
### **C++**
@@ -130,4 +148,20 @@ public:
130148
};
131149
```
132150

151+
### **Go**
152+
153+
```go
154+
func countCollisions(directions string) int {
155+
d := strings.TrimLeft(directions, "L")
156+
d = strings.TrimRight(d, "R")
157+
return len(d) - strings.Count(d, "S")
158+
}
159+
```
160+
161+
### **...**
162+
163+
```
164+
165+
```
166+
133167
<!-- tabs:end -->

‎solution/2200-2299/2211.Count Collisions on a Road/README_EN.md‎

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,41 +59,57 @@ No cars will collide with each other. Thus, the total number of collisions that
5959
```python
6060
class Solution:
6161
def countCollisions(self, directions: str) -> int:
62-
l, r = 0, len(directions)-1
63-
while l <= r and directions[l] == 'L':
64-
l += 1
65-
while l <= r and directions[r] == 'R':
66-
r -= 1
67-
count = 0
68-
for i in range(l, r+1):
69-
count += directions[i] != 'S'
70-
return count
62+
d = directions.lstrip('L').rstrip('R')
63+
return len(d) - d.count('S')
7164
```
7265

7366
### **Java**
7467

7568
```java
7669
class Solution {
7770
public int countCollisions(String directions) {
78-
int l = 0, r = directions.length() - 1, count = 0;
79-
while (l <= r && directions.substring(l, l + 1).equals("L")) {
80-
l++;
71+
char[] ds = directions.toCharArray();
72+
int n = ds.length;
73+
int l = 0;
74+
int r = n - 1;
75+
while (l < n && ds[l] == 'L') {
76+
++l;
8177
}
82-
while (l <= r && directions.substring(r, r +1).equals("R")) {
83-
r--;
78+
while (r >=0 && ds[r] =='R') {
79+
--r;
8480
}
85-
for (int i = l; i <= r; i++) {
86-
if (!directions.substring(i, i + 1).equals("S")) count += 1;
81+
int ans = 0;
82+
for (int i = l; i <= r; ++i) {
83+
if (ds[i] != 'S') {
84+
++ans;
85+
}
8786
}
88-
return count;
87+
return ans;
8988
}
9089
}
9190
```
9291

9392
### **TypeScript**
9493

9594
```ts
96-
95+
function countCollisions(directions: string): number {
96+
const n = directions.length;
97+
let l = 0,
98+
r = n - 1;
99+
while (l < n && directions[l] == 'L') {
100+
++l;
101+
}
102+
while (r >= 0 && directions[r] == 'R') {
103+
--r;
104+
}
105+
let ans = 0;
106+
for (let i = l; i <= r; ++i) {
107+
if (directions[i] != 'S') {
108+
++ans;
109+
}
110+
}
111+
return ans;
112+
}
97113
```
98114

99115
### **C++**
@@ -118,4 +134,20 @@ public:
118134
};
119135
```
120136

137+
### **Go**
138+
139+
```go
140+
func countCollisions(directions string) int {
141+
d := strings.TrimLeft(directions, "L")
142+
d = strings.TrimRight(d, "R")
143+
return len(d) - strings.Count(d, "S")
144+
}
145+
```
146+
147+
### **...**
148+
149+
```
150+
151+
```
152+
121153
<!-- tabs:end -->
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
func countCollisions(directions string) int {
2+
d := strings.TrimLeft(directions, "L")
3+
d = strings.TrimRight(d, "R")
4+
return len(d) - strings.Count(d, "S")
5+
}
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
class Solution {
22
public int countCollisions(String directions) {
3-
int l = 0, r = directions.length() - 1, count = 0;
4-
while (l <= r && directions.substring(l, l + 1).equals("L")) {
5-
l++;
3+
char[] ds = directions.toCharArray();
4+
int n = ds.length;
5+
int l = 0;
6+
int r = n - 1;
7+
while (l < n && ds[l] == 'L') {
8+
++l;
69
}
7-
while (l <= r && directions.substring(r, r + 1).equals("R")) {
8-
r--;
10+
while (r >= 0 && ds[r] == 'R') {
11+
--r;
912
}
10-
for (int i = l; i <= r; i++) {
11-
if (!directions.substring(i, i + 1).equals("S")) count += 1;
13+
int ans = 0;
14+
for (int i = l; i <= r; ++i) {
15+
if (ds[i] != 'S') {
16+
++ans;
17+
}
1218
}
13-
return count;
19+
return ans;
1420
}
15-
}
21+
}
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
class Solution:
22
def countCollisions(self, directions: str) -> int:
3-
l, r = 0, len(directions)-1
4-
while l <= r and directions[l] == 'L':
5-
l += 1
6-
while l <= r and directions[r] == 'R':
7-
r -= 1
8-
count = 0
9-
for i in range(l, r+1):
10-
count += directions[i] != 'S'
11-
return count
3+
d = directions.lstrip('L').rstrip('R')
4+
return len(d) - d.count('S')
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function countCollisions(directions: string): number {
2+
const n = directions.length;
3+
let l = 0,
4+
r = n - 1;
5+
while (l < n && directions[l] == 'L') {
6+
++l;
7+
}
8+
while (r >= 0 && directions[r] == 'R') {
9+
--r;
10+
}
11+
let ans = 0;
12+
for (let i = l; i <= r; ++i) {
13+
if (directions[i] != 'S') {
14+
++ans;
15+
}
16+
}
17+
return ans;
18+
}

‎solution/2200-2299/2213.Longest Substring of One Repeating Character/README.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ class SegmentTree:
150150
root.mx = max(root.mx, left.rmx + right.lmx)
151151

152152
def pushup(self, u):
153-
root = tr[u]
154-
left, right = tr[u << 1], tr[u << 1 | 1]
155-
self._pushup(root, left, right)
153+
self._pushup(tr[u], tr[u << 1], tr[u << 1 | 1])
156154

157155

158156
class Solution:

‎solution/2200-2299/2213.Longest Substring of One Repeating Character/README_EN.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ class SegmentTree:
129129
root.mx = max(root.mx, left.rmx + right.lmx)
130130

131131
def pushup(self, u):
132-
root = tr[u]
133-
left, right = tr[u << 1], tr[u << 1 | 1]
134-
self._pushup(root, left, right)
132+
self._pushup(tr[u], tr[u << 1], tr[u << 1 | 1])
135133

136134

137135
class Solution:

‎solution/2200-2299/2213.Longest Substring of One Repeating Character/Solution.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ def _pushup(self, root, left, right):
7171
root.mx = max(root.mx, left.rmx + right.lmx)
7272

7373
def pushup(self, u):
74-
root = tr[u]
75-
left, right = tr[u << 1], tr[u << 1 | 1]
76-
self._pushup(root, left, right)
74+
self._pushup(tr[u], tr[u << 1], tr[u << 1 | 1])
7775

7876

7977
class Solution:

0 commit comments

Comments
(0)

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