在这里,我尝试了这个,但在某处有错误,我正在尝试修复它。
private int maxDistance(List<String> path, String node) {
int max = Integer.MAX_VALUE;
int cost = 0;
for ( int i = 0 ; i < path.size(); i++)
{
path.add(i, node);
if ( cost > max)
cost = max;
path.remove(i);
}
return max;
答案 0 :(得分:2)
您并未将cost
更改为任何内容,max
已经是最大可能值,因此您的代码实际上并没有做任何事情:
private int maxDistance(List<String> path, String node) {
int max = 0, cost = 0; // Checking for a max
path.add(node); // You only want to do this once, so do it outside the loop
for(int i = 0; i < path.size(); i++) {
// find the cost somehow, you didn't specify what 'cost' really is
if(cost > max)
max = cost;
}
return max;
}