public int canCompleteCircuit(int[] gas, int[] cost) {
        int begin = 0;
        int end = 0;
        int i = 0;
        int sum = 0;
        int length = gas.length;
        int temp = length;
        while(temp-- > 0)
        {
            sum += gas[i] - cost[i];
            if(sum < 0)
            {
                if(--begin < 0) begin = length-1;
                i = begin;
            }
            else
            {
                if(++end >length-1) end = 0;
                i = end ;
            }
        }
//        sum += gas[i] - cost[i];
        if(sum < 0) return -1;
        else return begin;
    }