日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > leetcode No134. Gas Station

leetcode No134. Gas Station

來源:程序員人生   發布時間:2017-02-04 09:09:43 閱讀次數:2554次

Question

There are N gas stations along a circular route, where the amount of gas at station i is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station’s index if you can travel around the circuit once, otherwise return ⑴.
在1個圈里面有N個汽油站,i站的汽油有gas[i]汽油?,F在有1輛無窮容量油箱的車,它從i站開到(i+1)需要耗費cost[i]汽油。如果這輛車可以走完這個圈,那末返回這個車的出發點,否者返回⑴.

Algorithm

  • 如果從A到不了B,那末A-B之間的任何1站都到不了B(B是從A開始第1個不能到達的站)
  • 如果各個站的油量總和超過需要消耗油量總和,則1定有解

Accepted Code

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int start=0,tank=0,total=0;
        for(int i=0;i<gas.size();i++)
        {
            tank+=gas[i]-cost[i];
            if(tank<0)
            {
                total+=tank;
                tank=0;
                start=i+1;
            }
        }
        return ((total+tank)<0)?-1:start;
    }
};

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲成人在线网站 | 中文字幕日韩av | 日本亚洲欧美在线 | 国产精品伦一区二区三级视频 | 国产一级黄色片免费看 | 久久精品成人一区二区三区蜜臀 | 日韩 欧美 中文 | 成人精品鲁一区一区二区 | 久久精品系列 | 日韩欧美黄色 | 看a黄大片 | 久草手机在线观看 | 第九色激情 | 日日精品| 在线视频h | 亚洲精品乱码久久久久久黑人 | 久热精品在线 | 毛片6 | 日韩精品一卡 | 久久久91| 国产伦精品一区二区三区在线 | 永久91嫩草亚洲精品人人 | 欧美一级片 | 黄色在线观看国产 | 天天看夜夜 | 在线视频日韩精品 | 日韩精品三级 | 国产日本亚洲香蕉视频 | 日韩精品一二三区 | 成人在线国产 | 超碰人人艹 | 美女视频黄的 | 婷婷综合一区 | 国产精品一区二区久久 | 国产在线免费 | 久久香视频 | 国产精品99久久久久久动医院 | 国产精品久久久久久久7电影 | 国产精品久久久久婷婷二区次 | 亚洲综合精品 | 久久激情免费视频 |