我的LeetCode代码仓:https://github.com/617076674/LeetCode
原题链接:https://leetcode-cn.com/problems/gas-station/description/
题目描述:
知识点:数组
思路一:逐个判断每个位置是否符合条件
对每个索引位置都依次判断是否能从该位置开始绕环路行驶一周。
时间复杂度和输入的数据有关,在最坏的情况下是O(n ^ 2),其中n为加油站的数量。空间复杂度是O(1)。
JAVA代码:
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int start = -1;
for(int i = 0; i < gas.length; i++){
if(gas[i]