首页 > 其他 > 详细

[LeetCode]134 Gas Station

时间:2015-01-09 01:51:35      阅读:283      评论:0      收藏:0      [点我收藏+]

https://oj.leetcode.com/problems/gas-station/

http://blog.csdn.net/linhuanmars/article/details/22706553

public class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        
        int len = gas.length;

        // Cost if run all stations        
        int allcost = 0;
        
        // Start point
        int start = 0;
        
        // How many gas left
        int left = 0;

        for (int i = 0 ; i < len ; i ++)
        {
            allcost = allcost + gas[i] - cost[i];
            left = left + gas[i] - cost[i];
            if (left < 0)
            {
                start = i + 1;
                left = 0;
            }
        }
        
        if (start == len || allcost < 0)
            return -1;
        else
            return start;
    }
}


[LeetCode]134 Gas Station

原文:http://7371901.blog.51cto.com/7361901/1600752

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!