intcanCompleteCircuit(vector<int>& gas, vector<int>& cost){ int vsize = gas.size(); vector<int> lefts,prob; int sum = 0; for (int i = 0; i < vsize; i++) { int t = gas[i] - cost[i]; sum += t; lefts.push_back(t); if (t >= 0) prob.push_back(i); } if (sum < 0)return-1;
//处理有可能是起点的编号集合 int num = prob.size(); int start = prob[0]; for (int i = 0; i < num; i++) { start = prob[i]; int oil = 0; int k = 0; while (k<vsize) { oil += lefts[(start + k) % vsize]; if (oil < 0) break; k++; } if (oil >= 0)return start; } return start; }