1184. Distance Between Bus Stops

A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops number i and (i + 1) % n.

The bus goes along both directions i.e. clockwise and counterclockwise.

Return the shortest distance between the given start and destination stops.

利用模运算搞一搞就可以了,求一个遍顺时针的,然后total-顺时针的等于逆时针的,顺时针和逆时针求最小

class Solution(object):
    def distanceBetweenBusStops(self, distance, start, destination):
        """
        :type distance: List[int]
        :type start: int
        :type destination: int
        :rtype: int
        """
        n = len(distance)
        ans = 0
        while start != destination:
            ans += distance[start]
            start = (start + 1) % n
            
        return min(ans, sum(distance) - ans)
原文地址:https://www.cnblogs.com/whatyouthink/p/13232064.html