无法找出卡塔方向缩小问题错误



我正在研究代码战争中的方向缩减问题,但我无法弄清楚它给我的错误。我知道有类似的情况,但是当我在Visual Studio Code上测试我的代码时,它可以完美运行,所以我不确定为什么codewars会给我这个错误。我得到的错误是:"北","南","南","东","西","北": 数组长度不同,预期。长度=0 实际.长度=6

这是我的代码。请记住,codewars 会为您测试它,因此实际上不需要我的主要方法:

import java.lang.*;
public class DirReduction {
public static String[] dirReduc(String[] arr) {
int directionNS = 0;
int directionEW = 0;
for(int i = 0; i < arr.length; i++){
if(arr[i] == "NORTH"){
directionNS++;
} else if(arr[i] == "SOUTH"){
directionNS--;
} else if(arr[i] == "EAST"){
directionEW++;
} else if(arr[i] == "WEST"){
directionEW--;
} else {
System.out.println("Invalid Direction.");
}
}

String[] reducArray;
if(directionNS == 0 && directionEW == 0){
reducArray = new String[arr.length];
System.arraycopy(arr, 0, reducArray, 0, arr.length);
} else {
reducArray = new String[Math.abs(directionNS + directionEW)];
if(directionNS > 0){
for(int i = 0; i < directionNS; i++){
reducArray[i] = "NORTH";
}
} else if(directionNS < 0){
for(int i = 0; i > directionNS; i--){
reducArray[i] = "SOUTH";
}
} 

if(directionEW > 0){
for(int i = 0; i < directionEW; i++){
reducArray[i] = "EAST";
}
} else if(directionEW < 0){
for(int i = 0; i > directionEW; i--){
reducArray[i] = "WEST";
}
} 
}
return reducArray;
}
public static void main(String[] args){
String[] a = {"NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH","WEST"};
String[] result = dirReduc(a);
for(int i = 0; i < result.length; i++){
System.out.println(result[i]);
}
}
}

我发现了四个错误。

1( 案例 "NORTH"、"SOUTH"、"SOUTH"、"EAST"、"WEST"、"NORTH" 应该回到你开始的地方,所以数组长度应该为 0,按照 Codewars 的要求。为了使它起作用,我摆脱了两个方向计数为 0 的特殊情况,并通过添加 0 和 0 来获取数组大小,让您的 else 情况来处理它。[此错误是您问题中提到的错误]

2(您对数组大小的计算有点偏差。例如,对于"SOUTH"EAST",它计算的大小为 0,因为它们抵消了。相反,您需要对绝对值求和,而不是取总和的绝对值。

3( 缩减数组中的东/西从位置 0 开始,因此覆盖北/南。在执行这些操作之前,我确保偏移到数组中。

4(你在for循环上变为负数的策略将尝试写入负索引,如果你有像南,东,南。我使用Math.abs保持积极的态度

这是结果方法。

public static String[] dirReduc(String[] arr) {
int directionNS = 0;
int directionEW = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == "NORTH") {
directionNS++;
} else if (arr[i] == "SOUTH") {
directionNS--;
} else if (arr[i] == "EAST") {
directionEW++;
} else if (arr[i] == "WEST") {
directionEW--;
} else {
System.out.println("Invalid Direction.");
}
}

String[] reducArray;
//removed special case for ending up back where one started, that will be made a 0 length array as it should be
reducArray = new String[Math.abs(directionNS) + Math.abs(directionEW)]; //note have to take abs of each so one does not cancel out the other
if (directionNS > 0) {
for (int i = 0; i < directionNS; i++) {
reducArray[i] = "NORTH";
}
} else if (directionNS < 0) {
for(int i = 0; i < Math.abs(directionNS); i++){//keep the i's positive so they work in the array easily
reducArray[i] = "SOUTH";
}
}

if (directionEW > 0) {
for (int i = 0; i < directionEW; i++) {
reducArray[i + Math.abs(directionNS)] = "EAST"; //note have to start where north south left off
}
} else if (directionEW < 0) {
for(int i = 0; i < Math.abs(directionEW); i++){
reducArray[i + Math.abs(directionNS)] = "WEST";
}
}
return reducArray;
}`

最新更新