Java递归回溯问题

发布于 2024-11-25 20:46:53 字数 1188 浏览 1 评论 0原文

在这个递归回溯问题上遇到一些麻烦:

“编写一个方法可分区,它接受整数列表作为参数,并使用递归回溯来发现该列表是否可以分为两个总和相等的子列表如果给定的列表可以被平均划分,你的方法应该返回 true,否则返回 false。”

例如,列表 [1, 2, 3] 可以分为子列表 [1, 2] 和 [3],因此它将产生“true”结果。

我的解决方案看起来是正确的,但无论如何都会返回 false。我不明白为什么。

public static boolean partitionable(List<Integer> list1) {
        List<Integer> list2 = new ArrayList<Integer>();
        return partitionable(list1, list2);
    }

public static boolean partitionable(List<Integer> list1, List<Integer> list2) {
    boolean finalAnswer = false;
    int sum1 = 0;
    int sum2 = 0;
    for (int i = 0; i < list1.size(); i++) {
        sum1 += list1.get(i);
    }
    for (int i = 0; i < list2.size(); i++) {
        sum2 += list2.get(i);
    }
    if (sum1 == sum2) {
        return true;
    } else {
        for (int i = 0; i < list1.size() - 1; i++) {
            int number = list1.remove(i);
            list2.add(number);
            finalAnswer = partitionable(list1, list2);
            list2.remove(list2.size() - 1);
            list1.add(i, number);
        }
    }
    return finalAnswer;
}

编辑:我修复了从 list1 中删除元素两次的问题。

Having some trouble with this recursive backtracking problem:

"Write a method partitionable that accepts a list of integers as a parameter and uses recursive backtracking to discover whether the list can be partitioned into two sub-lists of equal sum. Your method should return true if the given list can be partitioned equally, and false if not."

For example, the list [1, 2, 3] can be partitioned into the sublists [1, 2] and [3], so it would produce a result of "true."

My solution seems correct, but returns false no matter what. I don't understand why.

public static boolean partitionable(List<Integer> list1) {
        List<Integer> list2 = new ArrayList<Integer>();
        return partitionable(list1, list2);
    }

public static boolean partitionable(List<Integer> list1, List<Integer> list2) {
    boolean finalAnswer = false;
    int sum1 = 0;
    int sum2 = 0;
    for (int i = 0; i < list1.size(); i++) {
        sum1 += list1.get(i);
    }
    for (int i = 0; i < list2.size(); i++) {
        sum2 += list2.get(i);
    }
    if (sum1 == sum2) {
        return true;
    } else {
        for (int i = 0; i < list1.size() - 1; i++) {
            int number = list1.remove(i);
            list2.add(number);
            finalAnswer = partitionable(list1, list2);
            list2.remove(list2.size() - 1);
            list1.add(i, number);
        }
    }
    return finalAnswer;
}

EDIT: I fixed the problem of removing the element from list1 twice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

┾廆蒐ゝ 2024-12-02 20:46:53

您正在调用 list1.remove(i) 两次。这可能会扰乱您的算法,因为您要删除两个数字,并仅保存其中一个以添加到 list2 中。

如果它仍然不起作用,我还注意到您忽略了 list1 的最后一个元素作为转到 list2 的候选元素。我没有看到发生这种情况的算法原因:您应该尝试从 for 循环中删除 -1

You are calling list1.remove(i) twice. That will probably mess up your algorithm, because you're removing two numbers, and saving only one of them to add to list2.

If it's still not working, I've also noticed that you're neglecting the last element of list1 as a candidate to go over to list2. I don't see an algorithmic reason for this to happen: you should try removing the -1 from your for loop.

相权↑美人 2024-12-02 20:46:53

问题是调用 list1.remove(i) 两次。这行不通。

您从 list1 中删除了两个数字,并将其保存到 list2 中时,仅保存了 1 个数字。

Problem is of calling list1.remove(i) twice. This won't work.

You'r removing two numbers from list1 and while saving it in list2, you are saving only 1.

探春 2024-12-02 20:46:53

您的递归情况(else 块)应检查是否finalAnswer == true,如果是这种情况,则立即返回。否则,您将跳过它到返回 false 的情况,并最终在底部返回它。

这并不能解决整个问题,因为您还从 list1 中删除了一个项目两次。解决这两个问题应该会给你带来正确的解决方案。

Your recursive case (the else block) should check to see if finalAnswer == true, and return it immediately if that's the case. Otherwise, you'll skip over it onto cases where false is returned, and eventually return that at the bottom.

That won't solve the whole problem, since you're also removing an item from list1 twice. Fixing both should get you the right solution.

只为守护你 2024-12-02 20:46:53

抱歉,我没有直接回答你的问题,但我对所提出问题的理解需要不同的答案。

原来的问题是这样问的:
如果给定的列表可以平均分区,您的方法应该返回 true

并且您稍后声明这一点:
[1, 2, 3] 可以划分为子列表 [1, 2] 和 [3],因此它会产生“true”结果。

这对我来说是不正确的。正确的解决方案(暂时忽略递归回溯要求)是否是计算整数元素的数量,% 2并返回NOT result

换句话说:

List has three elements.  
Divide by 2, remainder 1.
Return 0, list is not equally dividable.

List has four elements.
Divide by 2, remainder 0.
Return 1, list is equally dividable.

请让我知道我在哪里误解了这个问题。

Excuse me for not answering your question directly, but my understanding of the question posed necessitates a different answer.

The original question asks this:
Your method should return true if the given list can be partitioned equally

And you later claim this:
[1, 2, 3] can be partitioned into the sublists [1, 2] and [3], so it would produce a result of "true."

This rings incorrect to me. Would the proper solution (ignoring the recursive backtracking requirement for a moment) be to count the number of integer elements, % 2 and return NOT result?

In other words:

List has three elements.  
Divide by 2, remainder 1.
Return 0, list is not equally dividable.

List has four elements.
Divide by 2, remainder 0.
Return 1, list is equally dividable.

Please let me know where I've misunderstood the question.

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