找到数组的后续3个元素的和。

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

Find sum of subsequent 3 elements of an array

问题

需要对动态添加到同一数组的数字进行三个连续元素的求和,如果和等于参数值,则返回true。以下是您提供的代码,它返回所需的输出,但对于一些测试用例失败(您没有提供确切的测试用例)。是否有人能告诉我我的程序可能会失败的确切情景?

import java.util.LinkedList;
import java.util.List;

public class Test {
    List<Integer> mergeList = new LinkedList<Integer>();
    List<List<Integer>> allList = new LinkedList<List<Integer>>();
    List<Integer> tail;
    int from = 0;
    int to = 0;

    public void addLast(int[] list) {
        allList.removeAll(allList);

        for (int i : list) {
            mergeList.add(i);
        }

        if (mergeList.size() > 0) {
            int j = 0;

            while (to < mergeList.size()) {
                from = j;
                to = j + 3;
                tail = mergeList.subList(from, to);
                j++;
                allList.add(tail);
            }
        }
    }

    public boolean containsSum3(int sum) {
        boolean retVal = false;

        for (List<Integer> sum3List : allList) {
            if (sum3List.stream().mapToInt(Integer::intValue).sum() == sum) {
                retVal = true;
            }
        }

        return retVal;
    }

    public static void main(String[] args) {
        Test s = new Test();

        s.addLast(new int[] { 1, 2, 3 });
        System.out.println(s.containsSum3(6));
        System.out.println(s.containsSum3(9));

        s.addLast(new int[] { 4 });
        System.out.println(s.containsSum3(9));

        s.addLast(new int[] { 5, 2 });
        System.out.println(s.containsSum3(11));

        s.addLast(new int[] { 0, -1 });
        System.out.println(s.containsSum3(7));
        System.out.println(s.containsSum3(2));
    }
}

输出:

true
false
true
true
true
false

以上是您提供的代码,它的功能是将数字添加到列表中,然后检查是否存在三个连续元素的和等于给定参数值。这个代码片段的输出显示了每个测试情况下的结果。

英文:

I need to sum the three consecutive elements of an array when appending numbers to the same array dynamically and return true if the sum is equal to the argument value. I have already written the code below and it all return required output but it fails for some test cases( I don't have the the exact test cases), Can anybody tell me what exact scenario which my programme can be failed?

import java.util.LinkedList;
import java.util.List;
public class Test {
List&lt;Integer&gt; mergeList = new LinkedList&lt;Integer&gt;();
List&lt;List&lt;Integer&gt;&gt; allList = new LinkedList&lt;List&lt;Integer&gt;&gt;();
List&lt;Integer&gt; tail;
int from = 0;
int to = 0;
public void addLast(int[] list) {
allList.removeAll(allList);
for(int i : list) {
mergeList.add(i);
}
if (mergeList.size() &gt; 0) {
int j = 0;
while(to &lt; mergeList.size()){
from = j;
to = j + 3;
tail = mergeList.subList(from, to);
j++;
allList.add(tail);
}
}
}
public boolean containsSum3(int sum) {
boolean retVal = false;
for (List&lt;Integer&gt; sum3List : allList) {
if (sum3List.stream().mapToInt(Integer::intValue).sum() == sum) {
retVal = true;
}
}
return retVal;
}
public static void main(String[] args) {
Test s = new Test();
s.addLast(new int[] { 1, 2, 3 });
System.out.println(s.containsSum3(6));
System.out.println(s.containsSum3(9));
s.addLast(new int[] { 4 });
System.out.println(s.containsSum3(9));
s.addLast(new int[] { 5, 2});
System.out.println(s.containsSum3(11));
s.addLast(new int[] { 0, -1 });
System.out.println(s.containsSum3(7));
System.out.println(s.containsSum3(2));
}
}

Output:

true
false
true
true
true
false

答案1

得分: 1

以下是代码部分的中文翻译:

我生成了大量的随机整数集合,除了元素不足的情况外,无法找到您的代码失败的明显情况。顺便说一下,我编写的用于检查列表是否具有连续的n个元素总和等于给定值的函数是:

public static boolean containsSum(List<Integer> list, int sum, int n) {
    return IntStream.range(0, list.size() - n + 1)
            .anyMatch(i -> list.subList(i, i + n).stream()
                    .reduce(0, Integer::sum) == sum);
}

我看不出您的代码为什么需要保留所有列表的列表:空间/时间权衡并不太合理。我建议您将addLast 简化为将元素直接添加到mergeList。您的代码存在一些风格问题,但我相信您会在自己的时间内解决这些问题。

英文:

I generated large random collections of integers and couldn't find any obvious cases your code fails for beyond the insufficient elements. Incidentally, the function I wrote to check if a list has any consecutive n elements that sum to a given value was:

public static boolean containsSum(List&lt;Integer&gt; list, int sum, int n) {
return IntStream.range(0, list.size() - n + 1)
.anyMatch(i -&gt; list.subList(i, i + n).stream()
.reduce(0, Integer::sum) == sum);
}

I can't see any reason for your code that keeps all the list of lists: the space / time tradeoff doesn't make a lot of sense. I suggest you could simplify addLast to just add the elements to mergeList. There are a bunch of stylistic issues with your code but I'm sure you'll work those out in your own time.

huangapple
  • 本文由 发表于 2023年2月10日 10:54:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406485.html
匿名

发表评论

匿名网友

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

确定