鉴于INT数组中的两个元素的总和,如何找到其最小的索引?
我想返回一个2个整数的阵列,包含一对整数的索引 的总和为k 。
int[] numbers = new int[]{1, 5, 8, 1, 2};
int k = 13;
我已经完成了此方法:
public int[] findSumPair(int[] numbers, int k){
int[] pairs = new int[2];
for(int i = 0; i < numbers.length; i++){
for(int j = i + 1; j < numbers.length; j++){
if (numbers[i] + numbers[j] == k){
pairs = new int[]{i, j};
if (numbers[i] + numbers[j] != k){
pairs = new int[]{0, 0};
}
}
}
}
return pairs;
}
使用以前的数组并总和它有效&amp;返回:
[1,2]
例如,以下内容:
int[] numbers = new int[]{1, 5, 0, 1, 2, 7, 8, 6};
int k = 13;
我应该有:
[1,6] // But I get [5,7]
如果有几个可能的对等于目标的对,我该如何返回以最低左索引返回该对?
如果有2对具有相同的左索引,请选择右最低索引的对?
非常感谢
I want to return an array of 2 integers, containing the indexes of a pair of integers in the array whose sum is k.
int[] numbers = new int[]{1, 5, 8, 1, 2};
int k = 13;
I have done this method :
public int[] findSumPair(int[] numbers, int k){
int[] pairs = new int[2];
for(int i = 0; i < numbers.length; i++){
for(int j = i + 1; j < numbers.length; j++){
if (numbers[i] + numbers[j] == k){
pairs = new int[]{i, j};
if (numbers[i] + numbers[j] != k){
pairs = new int[]{0, 0};
}
}
}
}
return pairs;
}
With previous array and sum it works & returns:
[1,2]
For example with this:
int[] numbers = new int[]{1, 5, 0, 1, 2, 7, 8, 6};
int k = 13;
I should have:
[1,6] // But I get [5,7]
How can I, in case there are several possible pairs whose sum is equal to the target, return the pair with the lowest left index?
And in the case of 2 pairs with the same left index, go for the pair with the lowest right index?
Many thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设
的返回[N,M]
是一个有效的输出,其中n
&lt;m
和m
&gt; 0。假设返回[0,0]
是一个异常输出,表明找不到这对。您的功能存在一些错误:
首先,如果您想找到最小的索引,一旦找到匹配的对,就可以打破前面的索引。由于您开始从左侧搜索,因此第一匹匹配必须是最小的。无论现在艰难的程序尝试,下一个结果都不是最小的。
其次,我认为您尝试设置错误的情况条件,以返回
[0,0]
如果找不到一对。但是,该条件与IF的外部相矛盾。那是一个无法实现的陈述。您可以将其放在功能的末尾,因为当我们到达该部分时,搜索已经完成。我将该函数修改为:main()
控制台输出
Assume a return of
[n,m]
is an valid output wheren
<>m
andm
> 0. Assume a return of[0,0]
is an exception output indicates the pair is not found.There are some mistake in your function:
Firstly, if you want to find the smallest index, once you found a matched pair, you could break the for-loop. Since you start searching from the left, the first match must be the smallest. No matter now hard the program try, the next result is not the smallest.
Secondly, I think you try to set a false case condition to return
[0,0]
if no pair is found. However, the condition is contradict to its outer if condition. That is a unreachable statement. You can put that at the very end of the function since when we reach that part, the searching is already completed. I modified the function as:the
main()
console output
由于您是从左到右扫描,因此您可以在找到第一个匹配IE之后
返回
,替换为
返回对
。Since you are scanning left to right, you can just
return
after finding the first match i.e., replacingwith
return pairs
.@bui
我必须保留此部分:
因为:
如果找不到k,必须返回
@bui
I have to keep this part:
Because:
Must be returned if no pair equal to k is found