鉴于INT数组中的两个元素的总和,如何找到其最小的索引?

发布于 2025-01-23 16:44:15 字数 945 浏览 2 评论 0原文

我想返回一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假面具 2025-01-30 16:44:15

假设的返回[N,M]是一个有效的输出,其中n&lt; mm &gt; 0。假设返回[0,0]是一个异常输出,表明找不到这对。

您的功能存在一些错误:

    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) {
                    // if a pair is found, you could stop looking anymore
                    pairs = new int[] { i, j };

                    // unreachable statement
                    if (numbers[i] + numbers[j] != k) {
                        pairs = new int[] { 0, 0 };
                    }
                }
            }
        }
        return pairs;
    }

首先,如果您想找到最小的索引,一旦找到匹配的对,就可以打破前面的索引。由于您开始从左侧搜索,因此第一匹匹配必须是最小的。无论现在艰难的程序尝试,下一个结果都不是最小的。

其次,我认为您尝试设置错误的情况条件,以返回[0,0]如果找不到一对。但是,该条件与IF的外部相矛盾。那是一个无法实现的陈述。您可以将其放在功能的末尾,因为当我们到达该部分时,搜索已经完成。我将该函数修改为:

    public int[] findSumPair(int[] numbers, int k) {

        for (int i = 0; i < numbers.length; i++) {
            for (int j = i + 1; j < numbers.length; j++) {
                if (numbers[i] + numbers[j] == k) {
                    // quit immediately
                    return new int[] { i, j };
                }
            }
        }
        // if we reach here, no pair is found
        return new int[2];
    }

main()

        int[] result = main.findSumPair(new int[] { 1, 5, 8, 1, 2 }, 13);
        System.out.println(Arrays.toString(result));

        result = main.findSumPair(new int[] { 1, 5, 0, 1, 2, 7, 8, 6 }, 13);
        System.out.println(Arrays.toString(result));

        result = main.findSumPair(new int[] { 1, 2, 2, 2, 2, 1 }, 2);
        System.out.println(Arrays.toString(result));

        result = main.findSumPair(new int[] { 1, 2, 2, 2, 4, 3 }, 7);
        System.out.println(Arrays.toString(result));

        result = main.findSumPair(new int[] { 1, 2, 3, 9, 9, 9, 4 }, 5);
        System.out.println(Arrays.toString(result));

控制台输出

[1, 2]
[5, 7]
[0, 5]
[4, 5]
[0, 6]

Assume a return of [n,m] is an valid output where n<>m and m > 0. Assume a return of [0,0] is an exception output indicates the pair is not found.

There are some mistake in your function:

    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) {
                    // if a pair is found, you could stop looking anymore
                    pairs = new int[] { i, j };

                    // unreachable statement
                    if (numbers[i] + numbers[j] != k) {
                        pairs = new int[] { 0, 0 };
                    }
                }
            }
        }
        return pairs;
    }

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:

    public int[] findSumPair(int[] numbers, int k) {

        for (int i = 0; i < numbers.length; i++) {
            for (int j = i + 1; j < numbers.length; j++) {
                if (numbers[i] + numbers[j] == k) {
                    // quit immediately
                    return new int[] { i, j };
                }
            }
        }
        // if we reach here, no pair is found
        return new int[2];
    }

the main()

        int[] result = main.findSumPair(new int[] { 1, 5, 8, 1, 2 }, 13);
        System.out.println(Arrays.toString(result));

        result = main.findSumPair(new int[] { 1, 5, 0, 1, 2, 7, 8, 6 }, 13);
        System.out.println(Arrays.toString(result));

        result = main.findSumPair(new int[] { 1, 2, 2, 2, 2, 1 }, 2);
        System.out.println(Arrays.toString(result));

        result = main.findSumPair(new int[] { 1, 2, 2, 2, 4, 3 }, 7);
        System.out.println(Arrays.toString(result));

        result = main.findSumPair(new int[] { 1, 2, 3, 9, 9, 9, 4 }, 5);
        System.out.println(Arrays.toString(result));

console output

[1, 2]
[5, 7]
[0, 5]
[4, 5]
[0, 6]
糖粟与秋泊 2025-01-30 16:44:15

由于您是从左到右扫描,因此您可以在找到第一个匹配IE之后返回,替换

           if (numbers[i] + numbers[j] != k){
               pairs = new int[]{0, 0};
           }

返回对

Since you are scanning left to right, you can just return after finding the first match i.e., replacing

           if (numbers[i] + numbers[j] != k){
               pairs = new int[]{0, 0};
           }

with return pairs.

她说她爱他 2025-01-30 16:44:15

@bui

我必须保留此部分:

if (numbers[i] + numbers[j] != k) {
    pairs = new int[]{0, 0};
}

因为:

[0, 0] 

如果找不到k,必须返回

@bui

I have to keep this part:

if (numbers[i] + numbers[j] != k) {
    pairs = new int[]{0, 0};
}

Because:

[0, 0] 

Must be returned if no pair equal to k is found

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文