leetcode 134加油站

 

 

leetcode 134加油站问题,不太理解bug在哪,先挂着,我写的在Class Solution部分,但是报错在line51

更新:发现漏了个括号,,,果然发烧人就晕……已经更正

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int re=-1,i=0,j=0;
        int l=gas.size();
        while(re==-1&&i<l){
            gas.push_back(gas[i]);//下标每次后移前都把当前下标数目push_back
            cost.push_back(cost[i]);
            if(gas[i]>=cost[i]){//当前point满足出发条件进入子循环;
                int oil=gas[i],m=0;
                while(m<l-1){
                    if(oil-cost[i+m]>=0){
                        oil=oil-cost[i+m]+gas[i+m+1];
                        m++;
                    }else{
                        break;
                    }
                }
                if((m==4)&&(oil-cost[i+m]>=0)){
                    re=i;
                    break;
                }
            }
            i++;
        }
return re;
} };
void trimLeftTrailingSpaces(string &input) { input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) { return !isspace(ch); })); } void trimRightTrailingSpaces(string &input) { input.erase(find_if(input.rbegin(), input.rend(), [](int ch) { return !isspace(ch); }).base(), input.end()); } vector<int> stringToIntegerVector(string input) { vector<int> output; trimLeftTrailingSpaces(input); trimRightTrailingSpaces(input); input = input.substr(1, input.length() - 2); stringstream ss; ss.str(input); string item; char delim = ','; while (getline(ss, item, delim)) { output.push_back(stoi(item)); } return output; } int main() { string line; while (getline(cin, line)) { vector<int> gas = stringToIntegerVector(line); getline(cin, line); vector<int> cost = stringToIntegerVector(line); int ret = Solution().canCompleteCircuit(gas, cost); string out = to_string(ret); cout << out << endl; } return 0; }

 

posted @ 2019-01-16 19:59  Joel_Wang  阅读(244)  评论(0编辑  收藏  举报