t******d 发帖数: 1383 | 1 说是runtime error,有一个很大的case没过去。runtime error是说我的index过了界
么?还是说运行速度不够?请帮我看看。另外,reach+1改成 reach++,是一样的么
?把reach加了1之后的值传给新call的isEnough.我知道++reach,肯定是和reach+1等
效可以的。
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
for (int i = 0; i < gas.length;i++) {
if (isEnough(gas, cost, i, 0, i))
return i;
}
return -1;
}
public boolean isEnough(int[] gas, int[] cost, int start, int leftover,
int reach) {
if (reach == gas.length)
reach -= gas.length;
if (reach == start -1 || (start == 0 && reach == gas.length - 1)) {
if (leftover + gas[reach] >= cost[reach])
return true;
else
return false;
}
if (reach == start) {
if (gas[reach] < cost[reach])
return false;
else
return isEnough(gas, cost, start, gas[reach]-cost[reach],
reach+1);
}
if (leftover + gas[reach] >= cost[reach])
return isEnough(gas, cost, start, leftover + gas[reach]-cost[
reach], reach+1);
else return false;
}
} | c*****o 发帖数: 1702 | 2 run time error基本是segmentation error | t******d 发帖数: 1383 | 3 貌似小case过了。我也觉得我改用dp做的。
【在 c*****o 的大作中提到】 : run time error基本是segmentation error
| t**********r 发帖数: 2153 | | t******d 发帖数: 1383 | 5 那个没过的case,很大。2个数组,放main里面,都超过了size,jvm跑都不让跑!
【在 t**********r 的大作中提到】 : 自己debug一下呗
| t******d 发帖数: 1383 | 6 说是runtime error,有一个很大的case没过去。runtime error是说我的index过了界
么?还是说运行速度不够?请帮我看看。另外,reach+1改成 reach++,是一样的么
?把reach加了1之后的值传给新call的isEnough.我知道++reach,肯定是和reach+1等
效可以的。
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
for (int i = 0; i < gas.length;i++) {
if (isEnough(gas, cost, i, 0, i))
return i;
}
return -1;
}
public boolean isEnough(int[] gas, int[] cost, int start, int leftover,
int reach) {
if (reach == gas.length)
reach -= gas.length;
if (reach == start -1 || (start == 0 && reach == gas.length - 1)) {
if (leftover + gas[reach] >= cost[reach])
return true;
else
return false;
}
if (reach == start) {
if (gas[reach] < cost[reach])
return false;
else
return isEnough(gas, cost, start, gas[reach]-cost[reach],
reach+1);
}
if (leftover + gas[reach] >= cost[reach])
return isEnough(gas, cost, start, leftover + gas[reach]-cost[
reach], reach+1);
else return false;
}
} | c*****o 发帖数: 1702 | 7 run time error基本是segmentation error | t******d 发帖数: 1383 | 8 貌似小case过了。我也觉得我改用dp做的。
【在 c*****o 的大作中提到】 : run time error基本是segmentation error
| t**********r 发帖数: 2153 | | t******d 发帖数: 1383 | 10 那个没过的case,很大。2个数组,放main里面,都超过了size,jvm跑都不让跑!
【在 t**********r 的大作中提到】 : 自己debug一下呗
| b********r 发帖数: 620 | 11 贴个自己写的,所有test都pass了。不知道怎么用地跪。恳请大牛批评指正。
public static int selectGasStation(int[] stationGasAvailability, int[]
requiredGasAvailabilityForDriving) {
if (stationGasAvailability.length == 0 ||
requiredGasAvailabilityForDriving.length == 0) {
return -1;
} else if (stationGasAvailability.length == 1) {
// if there is only one station we can always do
return 0;
}
// check total availability vs total requirement
int totalAvailability = 0;
int totalRequirement = 0;
for (int i = 0;i < stationGasAvailability.length;i ++) {
totalAvailability += stationGasAvailability[i];
totalRequirement += requiredGasAvailabilityForDriving[i];
}
if (totalAvailability < totalRequirement) {
// total availability must be greater than or equal to
consumption
return -1;
}
int[] diff = new int[stationGasAvailability.length];
for (int i = 0;i < stationGasAvailability.length;i ++) {
diff[i] = stationGasAvailability[i] -
requiredGasAvailabilityForDriving[i];
}
int index = 0;
int remaining = 0;
int startIndex = -1;
boolean first = true;
while (true) {
if (diff[index] + remaining >= 0) {
if (first) {
startIndex = index;
first = false;
}
remaining += diff[index];
} else {
first = true;
remaining = 0;
}
index = (index + 1) % stationGasAvailability.length;
if (first == false && startIndex == index) {
return startIndex;
} else if (index == 0 && remaining <= 0) {
return -1;
}
}
}
【在 t******d 的大作中提到】 : 那个没过的case,很大。2个数组,放main里面,都超过了size,jvm跑都不让跑!
|
|