首页 > 其他 > 详细

LeetCode – Refresh – Gas Station

时间:2015-03-20 01:10:52      阅读:291      评论:0      收藏:0      [点我收藏+]

Not quite hard. Just remember initialize index to 0. Because if you initialize it as -1 and all the gas satisfy the cost, it will return -1.

Actually, it was 0.

 

 1 class Solution {
 2 public:
 3     int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
 4         int len = gas.size(), total = 0, sum = 0, index = 0;
 5         if (len == 0 || len != cost.size()) return -1;
 6         for (int i = 0; i < len; i++) {
 7             total += gas[i] - cost[i];
 8             sum += gas[i] - cost[i];
 9             if (sum < 0) {
10                 sum = 0;
11                 index = i + 1;
12             }
13         }
14         return total < 0 ? -1 : index;
15     }
16 };

 

LeetCode – Refresh – Gas Station

原文:http://www.cnblogs.com/shuashuashua/p/4352209.html

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