<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 一.題目描述 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. Note:? The solution is guaranteed to be unique. 環形路線上有N個加油站,每個加油站有汽油`gas[i]`,從每個加油站到下一站消耗汽油`cost[i]`,問從哪個加油站出發能夠回到起始點,如果都不能則返回-1,最后題目提到解是唯一的。 ## 二.題目分析 以下解法的時間復雜度為:`O(n)`,每個車站可加油量和每段距離的耗油量分別存放在`gas`和`cost`中,可以設置兩個變量:`sum`用于判斷當前是否有足夠的油量走到下一個加油站,若`sum<0`,則需將出發車站前移一個車站;`remainingGas`用于記錄車開完整個過程后的油量。最終,判斷`remainingGas`?是否大于零,是則返回初始車站的下標;若所有車站都出發都無法走完全程,則返回`-1`。 ## 三.示例代碼 ~~~ #include <vector> using namespace std; class Solution { public: int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int remainingGas = 0; int resultIndex = 0; int sum = 0; for (size_t i = 0; i < gas.size(); ++i) // 對每個車站的情況進行遍歷 { remainingGas += gas[i] - cost[i]; sum += gas[i] - cost[i]; if (sum < 0) { sum = 0; resultIndex = i + 1; } } if (remainingGas < 0) return -1; // 所有加油站的加油量小于消耗量 else return resultIndex; // 有解 } }; ~~~ 幾個處理結果: ![](https://box.kancloud.cn/2016-01-05_568bb5ebdf980.jpg) ![](https://box.kancloud.cn/2016-01-05_568bb5ebec2ed.jpg) ![](https://box.kancloud.cn/2016-01-05_568bb5ec04ef8.jpg) ## 四.小結 該題是比較有趣的一題,該方法只是多種方法中一種。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看