首页 > 其他 > 详细

Gas Station

时间:2015-06-12 06:28:47      阅读:178      评论:0      收藏:0      [点我收藏+]

我觉得这道题是dp比较有趣的应用吧,但是老用不熟

sigh一下

 

public class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int total=0, curRem=0, curSum=0,start=0;
        for(int i=0;i<gas.length;i++){
            curRem = gas[i]-cost[i];
            if(curSum>=0){
                curSum += curRem;
            }else{
                curSum = curRem;
                start = i;
            }
            total += curRem;
        }
        return total<0? -1: start;
    }
}

 

Gas Station

原文:http://www.cnblogs.com/jiajiaxingxing/p/4570728.html

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