59
59
60
60
<!-- 这里可写通用的实现逻辑 -->
61
61
62
- ** 方法一:一次遍历**
62
+ ** 方法一:模拟**
63
+
64
+ 我们可以先统计出公交车的总行驶距离 $s$,然后模拟公交车的行驶过程,从出发点开始,每次向右移动一站,直到到达目的地为止。在模拟的过程中,我们可以记录从出发点到目的地的距离 $a$,那么从目的地到出发点的最短距离就是 $\min(a, s - a)$。
65
+
66
+ 时间复杂度 $O(n)$,其中 $n$ 是公交车站的数量。空间复杂度 $O(1)$。
63
67
64
68
<!-- tabs:start -->
65
69
69
73
70
74
``` python
71
75
class Solution :
72
- def distanceBetweenBusStops (
73
- self , distance : List[int ], start : int , destination : int
74
- ) -> int :
75
- if start > destination:
76
- start, destination = destination, start
77
- a = sum (distance[start:destination])
78
- b = sum (distance[:start]) + sum (distance[destination:])
79
- return min (a, b)
76
+ def distanceBetweenBusStops (self , distance : List[int ], start : int , destination : int ) -> int :
77
+ a, n = 0 , len (distance)
78
+ while start != destination:
79
+ a += distance[start]
80
+ start = (start + 1 ) % n
81
+ return min (a, sum (distance) - a)
80
82
```
81
83
82
84
### ** Java**
@@ -86,18 +88,14 @@ class Solution:
86
88
``` java
87
89
class Solution {
88
90
public int distanceBetweenBusStops (int [] distance , int start , int destination ) {
89
- if (start > destination) {
90
- return distanceBetweenBusStops(distance, destination, start);
91
+ int s = Arrays . stream(distance). sum();
92
+ int n = distance. length;
93
+ int a = 0 ;
94
+ while (start != destination) {
95
+ a += distance[start];
96
+ start = (start + 1 ) % n;
91
97
}
92
- int a = 0 , b = 0 ;
93
- for (int i = 0 ; i < distance. length; ++ i) {
94
- if (i >= start && i < destination) {
95
- a += distance[i];
96
- } else {
97
- b += distance[i];
98
- }
99
- }
100
- return Math . min(a, b);
98
+ return Math . min(a, s - a);
101
99
}
102
100
}
103
101
```
@@ -108,15 +106,13 @@ class Solution {
108
106
class Solution {
109
107
public:
110
108
int distanceBetweenBusStops(vector<int >& distance, int start, int destination) {
111
- if (start > destination) return distanceBetweenBusStops(distance, destination, start);
112
- int a = 0, b = 0;
113
- for (int i = 0; i < distance.size(); ++i) {
114
- if (i >= start && i < destination)
115
- a += distance[ i] ;
116
- else
117
- b += distance[ i] ;
109
+ int s = accumulate(distance.begin(), distance.end(), 0);
110
+ int a = 0, n = distance.size();
111
+ while (start != destination) {
112
+ a += distance[ start] ;
113
+ start = (start + 1) % n;
118
114
}
119
- return min(a, b );
115
+ return min(a, s - a );
120
116
}
121
117
};
122
118
```
@@ -125,17 +121,19 @@ public:
125
121
126
122
```go
127
123
func distanceBetweenBusStops(distance []int, start int, destination int) int {
128
- if start > destination {
129
- return distanceBetweenBusStops(distance, destination, start)
124
+ s := 0
125
+ for _, x := range distance {
126
+ s += x
130
127
}
131
- a, b := 0, 0
132
- for i, v := range distance {
133
- if i >= start && i < destination {
134
- a += v
135
- } else {
136
- b += v
137
- }
128
+ a, n := 0, len(distance)
129
+ for start != destination {
130
+ a += distance[start]
131
+ start = (start + 1) % n
138
132
}
133
+ return min(a, s-a)
134
+ }
135
+
136
+ func min(a, b int) int {
139
137
if a < b {
140
138
return a
141
139
}
@@ -153,22 +151,36 @@ func distanceBetweenBusStops(distance []int, start int, destination int) int {
153
151
* @return {number}
154
152
*/
155
153
var distanceBetweenBusStops = function (distance , start , destination ) {
156
- if (start > destination) {
157
- return distanceBetweenBusStops (distance, destination, start);
158
- }
154
+ const s = distance .reduce ((a , b ) => a + b, 0 );
159
155
let a = 0 ;
160
- let b = 0 ;
161
- for (let i = 0 ; i < distance .length ; ++ i) {
162
- if (i >= start && i < destination) {
163
- a += distance[i];
164
- } else {
165
- b += distance[i];
166
- }
156
+ const n = distance .length ;
157
+ while (start != destination) {
158
+ a += distance[start];
159
+ start = (start + 1 ) % n;
167
160
}
168
- return Math .min (a, b );
161
+ return Math .min (a, s - a );
169
162
};
170
163
```
171
164
165
+ ### ** TypeScript**
166
+
167
+ ``` ts
168
+ function distanceBetweenBusStops(
169
+ distance : number [],
170
+ start : number ,
171
+ destination : number ,
172
+ ): number {
173
+ const s = distance .reduce ((a , b ) => a + b , 0 );
174
+ let a = 0 ;
175
+ const n = distance .length ;
176
+ while (start != destination ) {
177
+ a += distance [start ];
178
+ start = (start + 1 ) % n ;
179
+ }
180
+ return Math .min (a , s - a );
181
+ }
182
+ ```
183
+
172
184
### ** ...**
173
185
174
186
```
0 commit comments