Google FooBar代码在IDE中运行正常,但在所有测试案例中失败。

huangapple go评论59阅读模式
英文:

Google FooBar Code Running in IDE but failing all test cases

问题

以下是翻译好的内容:

我尝试解决的问题是:“嘿!我已经做过了。”
问题陈述在这里,但那并不是我的问题,因为我已经在我的集成开发环境(IDE)中创建了适用于我的解决方案。

当我在我的IDE中创建一个样本主函数来运行文件时,我能够正确返回两个测试用例的值。然而,当我在foobar中验证它时,我未能通过所有测试用例,包括两个已知的用例。

我尝试返回1,因为那是第二个测试用例的答案,并且能够通过三个测试用例。然而,当我运行我的代码但在第一行放置了return 1;(使我的所有代码无效),我仍然未能通过所有测试用例。这意味着,由于某种原因,即使它没有执行到我的代码,我仍然未能通过所有测试用例。然而,Google没有告诉我任何关于编译错误的信息 - 它只是说我未能通过测试用例。

以下是代码:

public class Solution {
    public static int solution(String n, int b) {
        int tick = 0;
        int[] arr = new int[1000000000];
        int ans = 0;
        while (true) {
            tick++;
            if (arr[Integer.parseInt(n)] == 0) {
                arr[Integer.parseInt(n)] = tick;
            } else {
                ans = tick - arr[Integer.parseInt(n)];
                //System.out.println(ans);
                return ans;
            }
            char[] ar;
            ar = n.toCharArray();
            Arrays.sort(ar);
            String i = String.valueOf(ar);
            String j = "";
            for (int a = (ar.length - 1); a >= 0; a--) {
                j += String.valueOf(ar[a]);
            }
            //System.out.println(i + " " + j);

            int x = Integer.valueOf(j);
            int y = Integer.valueOf(i);

            String a1 = Integer.toString(x);
            String b1 = Integer.toString(y);
            int p = 1;
            int xx = 0;
            int yy = 0;

            p = 0;
            for (int ii = a1.length() - 1; ii >= 0; ii--) {
                xx += Integer.parseInt(Character.toString(a1.charAt(ii))) * Math.pow(b, p);
                p++;
            }

            p = 0;
            for (int ii = b1.length() - 1; ii >= 0; ii--) {
                yy += Integer.parseInt(Character.toString(b1.charAt(ii))) * Math.pow(b, p);
                p++;
            }


            String zz = String.valueOf(xx - yy);

            String newn = Integer.toString(Integer.parseInt(zz), b);

            for (int ii = 0; ii < n.length() - newn.length(); ii++)
                newn = "0" + newn;
            n = newn;
            //System.out.println(n);
            //f = true;
        }

    }
}
英文:

The problem I'm trying to solve is ‘Hey! I already did that.’
The problem statement is here but that isn't really my problem, as I've already created a solution that works for me on my IDE.

When I create a sample main to run the file in my IDE, I'm able to return the correct values for both test cases. However, when I verify it in foobar, I fail all test cases, including the two known cases.

I tried returning just 1, since that's the answer to the second test case, and that's able to past three of the test cases. However, when I run my code but put return 1; in the first line (rendering all of my code useless), I still fail all test cases. That means, for some reason, even when it doesn't get to my code, I'm failing all the test cases. However, Google isn't telling me anything about a compile error - it just says I'm failing the test cases.

Here's the code:

public class Solution {
public static int solution(String n, int b) {
int tick = 0;
int[] arr = new int[1000000000];
int ans = 0;
while (true) {
tick++;
if (arr[Integer.parseInt(n)] == 0) {
arr[Integer.parseInt(n)] = tick;
} else {
ans = tick - arr[Integer.parseInt(n)];
//System.out.println(ans);
return ans;
}
char[] ar;
ar = n.toCharArray();
Arrays.sort(ar);
String i = String.valueOf(ar);
String j = &quot;&quot;;
for (int a = (ar.length - 1); a &gt;= 0; a--) {
j += String.valueOf(ar[a]);
}
//System.out.println(i + &quot; &quot; + j);
int x = Integer.valueOf(j);
int y = Integer.valueOf(i);
String a1 = Integer.toString(x);
String b1 = Integer.toString(y);
int p = 1;
int xx = 0;
int yy = 0;
p = 0;
for (int ii = a1.length() - 1; ii &gt;= 0; ii--) {
xx += Integer.parseInt(Character.toString(a1.charAt(ii))) * Math.pow(b, p);
p++;
}
p = 0;
for (int ii = b1.length() - 1; ii &gt;= 0; ii--) {
yy += Integer.parseInt(Character.toString(b1.charAt(ii))) * Math.pow(b, p);
p++;
}
String zz = String.valueOf(xx - yy);
String newn = Integer.toString(Integer.parseInt(zz), b);
for (int ii = 0; ii &lt; n.length() - newn.length(); ii++)
newn = &quot;0&quot; + newn;
n = newn;
//System.out.println(n);
//f = true;
}
}
}

答案1

得分: 1

我找到了答案,有两个方面 - 首先,我之所以得到这个错误是因为,尽管 foobar 不支持通配符导入,但他们需要具体的导入(在这种情况下是 java.util.Arrays)。然而,即使我这样做了,效率仍然不高,所以我将其改为了一个简单的 HashMap 解决方案,通过这种方式我能够通过所有的测试案例。

英文:

I found the answer, which was twofold - firstly, I was getting the error because, although foobar doesn't want wildcard imports, they do want specific imports (in this case, java.util.Arrays). However, when I did do that, it was still inefficient, so I changed it to a simple HashMap solution and I'm able to pass all test cases that way.

huangapple
  • 本文由 发表于 2020年9月30日 09:25:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64129682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定