首页 > 其他 > 详细

134.Gas Station

时间:2016-05-23 09:03:50      阅读:185      评论:0      收藏:0      [点我收藏+]
    /*
     * 134.Gas Station
     * 2016-5-22 by Mingyang
     * 刚开始自己做的时候,一个点一个点的算,时间超过了
     * 这里就一次过,每次从一个i出发,无论如何总的收入应该大于总的支出,不然不论怎么转也不行
     * 另外一旦每一次走不动了,起点都跳到下一个i
     */
    public int canCompleteCircuit(int[] gas, int[] cost) {
        if (gas==null|| cost==null||gas.length==0||cost.length==0||gas.length!=cost.length)
         return -1;       
        int sum = 0;  
        int total = 0;  
        int index = 0;  
        for(int i = 0; i < gas.length; i++){  
            sum += gas[i]-cost[i];  
            total += gas[i]-cost[i];  
            if(sum < 0){  
                index=i+1; 
                sum = 0;   
            } 
        }  
        if(total<0)
            return -1;  
        else
            return index;  
    }

 

134.Gas Station

原文:http://www.cnblogs.com/zmyvszk/p/5518645.html

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