“未知错误发生” 在LeetCode中使用Java – 无法提交或运行 –

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

"Unknown Error Occurred" using Java in Leetcode - Cannot Submit or Run -

问题

我是LeetCode的新手在尝试运行我的代码时一直遇到错误有人之前遇到过这种情况吗我已经尝试了许多不同的方法甚至使用了别人的代码而那些代码是可以正常工作的

```java
class Solution {
   public int[] twoSum(int[] nums, int target) {         
       for (int i = 0; i < nums.length; i++) {
           for (int j = i + 1; j < nums.length; j++) {
               int complement = target - nums[i];
               if (nums[j] == complement) {
                   return new int[] {i, j};
                }
            }
        }
            
        throw new IllegalArgumentException("找不到匹配");
    }
}

这是我的代码。有人能帮帮我吗?

“未知错误发生” 在LeetCode中使用Java – 无法提交或运行 –


<details>
<summary>英文:</summary>

I am new on LeetCode, and continue to get an error when trying to run my code. Has anyone encountered this before. I have tried many different methods and even used other peoples code that has worked. 

```java
class Solution {
   public int[] twoSum(int[] nums, int target) {         
       for (int i = 0; i &lt; nums.length; i++) {
           for (int j = i + 1; j &lt; nums.length; j++) {
               int complement = target - nums[i];
               if (nums[j] == complement) {
                   return new int[] {i, j};
                }
            }
        }
            
        throw new IllegalArgumentException(&quot;no match found&quot;);
    }
}

<!-- end snippet -->

This is my code. Can anyone help me?

“未知错误发生” 在LeetCode中使用Java – 无法提交或运行 –

答案1

得分: 1

你的代码已经很好了,不过我们可以使用 HashMap 来更高效地解决这个问题:

public class Solution {
    public static final int[] twoSum(
        final int[] nums, 
        final int target
    ) {
        int[] indices = new int[2];
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int index = 0; index < nums.length; index++) {
            if (map.get(target - nums[index]) != null) {
                indices[1] = index;
                indices[0] = map.get(target - nums[index]);
                return indices;
            }
            map.put(nums[index], index);
        }
        return indices;
    }
}
英文:

Your code works just fine, yet we can use a HashMap for solving the problem a bit more efficiently:

public class Solution {
    public static final int[] twoSum(
        final int[] nums, 
        final int target
    ) {
        int[] indices = new int[2];
        HashMap&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();
        for (int index = 0; index &lt; nums.length; index++) {
            if (map.get(target - nums[index]) != null) {
                indices[1] = index;
                indices[0] = map.get(target - nums[index]);
                return indices;
            }
            map.put(nums[index], index);
        }
        return indices;
    }
}

huangapple
  • 本文由 发表于 2020年10月10日 03:46:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64286381.html
匿名

发表评论

匿名网友

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

确定