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 67a2062

Browse files
committed
feat: add solutions to lc problem: No.1184
No.1184.Distance Between Bus Stops
1 parent 6b5cb3f commit 67a2062

File tree

7 files changed

+248
-13
lines changed

7 files changed

+248
-13
lines changed

‎solution/1100-1199/1184.Distance Between Bus Stops/README.md‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,111 @@
5959

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

62+
**方法一:一次遍历**
63+
6264
<!-- tabs:start -->
6365

6466
### **Python3**
6567

6668
<!-- 这里可写当前语言的特殊实现逻辑 -->
6769

6870
```python
69-
71+
class Solution:
72+
def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:
73+
if start > destination:
74+
start, destination = destination, start
75+
a = sum(distance[start: destination])
76+
b = sum(distance[:start]) + sum(distance[destination:])
77+
return min(a, b)
7078
```
7179

7280
### **Java**
7381

7482
<!-- 这里可写当前语言的特殊实现逻辑 -->
7583

7684
```java
85+
class Solution {
86+
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
87+
if (start > destination) {
88+
return distanceBetweenBusStops(distance, destination, start);
89+
}
90+
int a = 0, b = 0;
91+
for (int i = 0; i < distance.length; ++i) {
92+
if (i >= start && i < destination) {
93+
a += distance[i];
94+
} else {
95+
b += distance[i];
96+
}
97+
}
98+
return Math.min(a, b);
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
109+
if (start > destination) return distanceBetweenBusStops(distance, destination, start);
110+
int a = 0, b = 0;
111+
for (int i = 0; i < distance.size(); ++i)
112+
{
113+
if (i >= start && i < destination) a += distance[i];
114+
else b += distance[i];
115+
}
116+
return min(a, b);
117+
}
118+
};
119+
```
120+
121+
### **Go**
122+
123+
```go
124+
func distanceBetweenBusStops(distance []int, start int, destination int) int {
125+
if start > destination {
126+
return distanceBetweenBusStops(distance, destination, start)
127+
}
128+
a, b := 0, 0
129+
for i, v := range distance {
130+
if i >= start && i < destination {
131+
a += v
132+
} else {
133+
b += v
134+
}
135+
}
136+
if a < b {
137+
return a
138+
}
139+
return b
140+
}
141+
```
77142

143+
### **JavaScript**
144+
145+
```js
146+
/**
147+
* @param {number[]} distance
148+
* @param {number} start
149+
* @param {number} destination
150+
* @return {number}
151+
*/
152+
var distanceBetweenBusStops = function (distance, start, destination) {
153+
if (start > destination) {
154+
return distanceBetweenBusStops(distance, destination, start);
155+
}
156+
let a = 0;
157+
let b = 0;
158+
for (let i = 0; i < distance.length; ++i) {
159+
if (i >= start && i < destination) {
160+
a += distance[i];
161+
} else {
162+
b += distance[i];
163+
}
164+
}
165+
return Math.min(a, b);
166+
};
78167
```
79168

80169
### **...**

‎solution/1100-1199/1184.Distance Between Bus Stops/README_EN.md‎

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,100 @@
7474
### **Python3**
7575

7676
```python
77-
77+
class Solution:
78+
def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:
79+
if start > destination:
80+
start, destination = destination, start
81+
a = sum(distance[start: destination])
82+
b = sum(distance[:start]) + sum(distance[destination:])
83+
return min(a, b)
7884
```
7985

8086
### **Java**
8187

8288
```java
89+
class Solution {
90+
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
91+
if (start > destination) {
92+
return distanceBetweenBusStops(distance, destination, start);
93+
}
94+
int a = 0, b = 0;
95+
for (int i = 0; i < distance.length; ++i) {
96+
if (i >= start && i < destination) {
97+
a += distance[i];
98+
} else {
99+
b += distance[i];
100+
}
101+
}
102+
return Math.min(a, b);
103+
}
104+
}
105+
```
106+
107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
113+
if (start > destination) return distanceBetweenBusStops(distance, destination, start);
114+
int a = 0, b = 0;
115+
for (int i = 0; i < distance.size(); ++i)
116+
{
117+
if (i >= start && i < destination) a += distance[i];
118+
else b += distance[i];
119+
}
120+
return min(a, b);
121+
}
122+
};
123+
```
124+
125+
### **Go**
126+
127+
```go
128+
func distanceBetweenBusStops(distance []int, start int, destination int) int {
129+
if start > destination {
130+
return distanceBetweenBusStops(distance, destination, start)
131+
}
132+
a, b := 0, 0
133+
for i, v := range distance {
134+
if i >= start && i < destination {
135+
a += v
136+
} else {
137+
b += v
138+
}
139+
}
140+
if a < b {
141+
return a
142+
}
143+
return b
144+
}
145+
```
83146

147+
### **JavaScript**
148+
149+
```js
150+
/**
151+
* @param {number[]} distance
152+
* @param {number} start
153+
* @param {number} destination
154+
* @return {number}
155+
*/
156+
var distanceBetweenBusStops = function (distance, start, destination) {
157+
if (start > destination) {
158+
return distanceBetweenBusStops(distance, destination, start);
159+
}
160+
let a = 0;
161+
let b = 0;
162+
for (let i = 0; i < distance.length; ++i) {
163+
if (i >= start && i < destination) {
164+
a += distance[i];
165+
} else {
166+
b += distance[i];
167+
}
168+
}
169+
return Math.min(a, b);
170+
};
84171
```
85172

86173
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
4+
if (start > destination) return distanceBetweenBusStops(distance, destination, start);
5+
int a = 0, b = 0;
6+
for (int i = 0; i < distance.size(); ++i)
7+
{
8+
if (i >= start && i < destination) a += distance[i];
9+
else b += distance[i];
10+
}
11+
return min(a, b);
12+
}
13+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func distanceBetweenBusStops(distance []int, start int, destination int) int {
2+
if start > destination {
3+
return distanceBetweenBusStops(distance, destination, start)
4+
}
5+
a, b := 0, 0
6+
for i, v := range distance {
7+
if i >= start && i < destination {
8+
a += v
9+
} else {
10+
b += v
11+
}
12+
}
13+
if a < b {
14+
return a
15+
}
16+
return b
17+
}
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
class Solution {
2-
public static int distanceBetweenBusStops(int[] distance, int start, int destination) {
3-
int length = 0;
4-
for (int i : distance) {
5-
length += i;
2+
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
3+
if (start > destination) {
4+
return distanceBetweenBusStops(distance, destination, start);
65
}
7-
int min = Math.min(start, destination);
8-
int max = Math.max(start, destination);
9-
int length2 = 0;
10-
for (int i = min; i < max; i++) {
11-
length2 += distance[i];
6+
int a = 0, b = 0;
7+
for (int i = 0; i < distance.length; ++i) {
8+
if (i >= start && i < destination) {
9+
a += distance[i];
10+
} else {
11+
b += distance[i];
12+
}
1213
}
13-
return Math.min(length - length2, length2);
14+
return Math.min(a, b);
1415
}
15-
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} distance
3+
* @param {number} start
4+
* @param {number} destination
5+
* @return {number}
6+
*/
7+
var distanceBetweenBusStops = function (distance, start, destination) {
8+
if (start > destination) {
9+
return distanceBetweenBusStops(distance, destination, start);
10+
}
11+
let a = 0;
12+
let b = 0;
13+
for (let i = 0; i < distance.length; ++i) {
14+
if (i >= start && i < destination) {
15+
a += distance[i];
16+
} else {
17+
b += distance[i];
18+
}
19+
}
20+
return Math.min(a, b);
21+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:
3+
if start > destination:
4+
start, destination = destination, start
5+
a = sum(distance[start: destination])
6+
b = sum(distance[:start]) + sum(distance[destination:])
7+
return min(a, b)

0 commit comments

Comments
(0)

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