英文:
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<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<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; // 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<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.
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论