这个 return 语句放在哪里是正确的位置?

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

Where is the correct place to put this return statement?

问题

class Solution {
    
    public int[] twoSum(int[] nums, int target) {
        
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] + nums[i++] == target) {
                int[] solutions = new int[] {nums[i], nums[i++]};
                return solutions;
            }
        }
    }
}
英文:

I am trying to solve the simple two sum problem. However, my trouble is in figuring out where to put the return statement that returns the array. If I put the return statement within the if statement I get a missing return statement error. If I put the return statement right after the if statement, then I get a cannot find symbol error. If I put the return statement right before the end of the method, I get the same cannot find symbol error. I cannot add an else statement because there is no other array that I wish to return if the condition in the if statement is not met, and I am required to return an array. It seems that wherever I put this return statement I get an error. Is there a correct place to put it or is my implementation totally wrong.
I would really appreciate suggestions from anyone.
Thank you!

Note: I understand that this solution probably doesn't solve the two sum problem but I would just like to get this code running so that I can see what is wrong with my logic so that I can improve it.

class Solution {
    
    public int[] twoSum(int[] nums, int target) {
        
        for (int i=0; i&lt;nums.length; i++) {
            if(nums[i] + nums[i++] == target) {
                int[] solutions = new int[] {nums[i], nums[i++]};
                return solutions;
            }
        }
    }
}

答案1

得分: 0

只需将 return 语句放在循环内,并在方法末尾添加任何 error 标记 - null 值或 throw RuntimeException()

/**
 * 返回一个包含两个元素的数组,如果未找到则返回 {@literal null}
 */
public int[] twoSum(int[] nums, int target) {
    Set<Integer> unique = new HashSet<>();

    for (int i = 0; i < nums.length; i++) {
        int diff = target - nums[i];

        if (unique.contains(diff))
            return new int[] { nums[i], diff };

        unique.add(nums[i]);
    }

    return null;  // 可以是:一个空数组,异常,...
}
英文:

Just place return statement inside a loop and at the end of the method any error marker - null value or throw RuntimeException().

/**
 * @return array with exactly two elements or {@literal null} if not found
 */
public int[] twoSum(int[] nums, int target) {
    Set&lt;Integer&gt; unique = new HashSet&lt;&gt;();

    for (int i = 0; i &lt; nums.length; i++) {
        int diff = target - nums[i];

        if (unique.contains(diff))
            return new int[] { nums[i], diff };

        unique.add(nums[i]);
    }

    return null;  // could be: an empty array, exception,...
}

答案2

得分: 0

如果您的函数被定义为返回一个 int[] 数组您必须在任何情况下都返回它而不仅仅在特定条件下返回

我对Java不太了解但在结构上以下是我认为可以解决您问题的更改已在评论中标出):

class Solution {

    public int[] twoSum(int[] nums, int target) {
        // 在这里定义一个变量,它包含您将返回的内容。
        // 给它一个空值,或者当条件不满足时将返回的虚拟值。

        for (int i=0; i<nums.length; i++) {
            if(nums[i] + nums[i++] == target) {
                // 在这里应该给数组分配值,而不是创建
                int[] solutions = new int[] {nums[i], nums[i++]};
                return solutions; // 从这里移除 return
            }
        }
        // 在这里,在函数结束之前,应该有一个返回语句。
    }
}
英文:

if your function is defined to return an int[] you have to return it in any case, not only if a particular condition applies.

I am not good at Java, but structure wise, here you have changes I think would solve your issue in comments:

class Solution {
    
    public int[] twoSum(int[] nums, int target) {
        // Define here variable which contains what you would return. 
        // Give it a null, or dummy value what would be returned if your condition is not met.
           
        for (int i=0; i&lt;nums.length; i++) {
            if(nums[i] + nums[i++] == target) {
                // Here you should assign values to the array, not create
                                int[] solutions = new int[] {nums[i], nums[i++]};
                return solutions; // remove return from here
            }
        }
        // here right before closing the function, is where return should be.
        
    }
}

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

发表评论

匿名网友

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

确定