两个数组的交集,超低效率栈法。。。

2501_91783423 2025-05-11 20:39:39

豆包AI支持 

import java.util.*;

public int[] intersection(int[] nums1, int[] nums2) {
    // 1. 将数组转换为栈(栈顶为数组最后一个元素)
    Stack<Integer> stack1 = arrayToStack(nums1);
    Stack<Integer> stack2 = arrayToStack(nums2);

    // 2. 创建临时栈用于存储共同元素
    Stack<Integer> commonStack = new Stack<>();

    // 3. 复制stack1到temp1(避免修改原栈)
    Stack<Integer> temp1 = new Stack<>();
    temp1.addAll(stack1);

    // 4. 遍历stack2(元素顺序与原数组相反)
    Stack<Integer> temp2 = new Stack<>();
    while (!stack2.isEmpty()) {
        int num = stack2.pop(); // 弹出stack2的栈顶元素

        // 5. 检查temp1中是否存在该元素(时间复杂度O(n))
        if (contains(temp1, num)) {
            commonStack.push(num); // 存在则加入共同元素栈
        }

        // 6. 使用temp2保存stack2的弹出元素,以便后续恢复
        temp2.push(num);
    }

    // 7. 恢复stack2的原始顺序(元素顺序与原数组相反)
    while (!temp2.isEmpty()) {
        stack2.push(temp2.pop());
    }

    // 8. 将共同元素栈转换为Set去重,并转为数组返回
    Set<Integer> uniqueCommon = new HashSet<>(commonStack);
    int[] result = new int[uniqueCommon.size()];
    int index = 0;
    for (int num : uniqueCommon) {
        result[index++] = num;
    }

    return result;
}

private Stack<Integer> arrayToStack(int[] arr) {
    Stack<Integer> stack = new Stack<>();
    for (int num : arr) {
        stack.push(num); // 按数组顺序压栈,栈顶为数组最后一个元素
    }
    return stack;
}
private boolean contains(Stack<Integer> stack, int target) {
    Stack<Integer> temp = new Stack<>();
    boolean found = false;

    // 1. 遍历栈查找元素(时间复杂度O(n))
    while (!stack.isEmpty()) {
        int num = stack.pop();
        if (num == target) {
            found = true;
        }
        temp.push(num); // 保存元素到临时栈
    }

    // 2. 恢复原栈的顺序(元素顺序不变)
    while (!temp.isEmpty()) {
        stack.push(temp.pop());
    }

    return found;
}

举个例子(豆包举例

数组转换为栈:
对于 nums1,调用 arrayToStack(nums1) 后,stack1 为 [1, 2, 3](栈顶为 3)。
对于 nums2,调用 arrayToStack(nums2) 后,stack2 为 [2, 3, 4](栈顶为 4)。
创建临时栈和复制操作:
创建 commonStack 用于存储共同元素,初始为空栈。
复制 stack1 到 temp1,temp1 为 [1, 2, 3](栈顶为 3)。
遍历 stack2 并检查元素:
开始遍历 stack2,stack2 栈顶元素 num = 4,调用 contains(temp1, 4):
contains 方法中,temp 初始为空栈,found 为 false。
遍历 temp1,temp1 栈顶元素为 3,不等于 4,将 3 压入 temp,继续遍历。
temp1 栈顶元素为 2,不等于 4,将 2 压入 temp,继续遍历。
temp1 栈顶元素为 1,不等于 4,将 1 压入 temp,遍历结束,found 仍为 false,contains 方法返回 false,commonStack 不变。
将 4 压入 temp2。
stack2 栈顶元素 num = 3,调用 contains(temp1, 3):
contains 方法中,temp 初始为 [1, 2, 3](temp1 恢复后的状态),found 为 false。
遍历 temp1,temp1 栈顶元素为 3,等于 3,found 变为 true,将 3 压入 commonStack,contains 方法返回 true。
将 3 压入 temp2。
stack2 栈顶元素 num = 2,调用 contains(temp1, 2):
contains 方法中,temp 初始为 [1, 2](temp1 恢复后的状态),found 为 false。
遍历 temp1,temp1 栈顶元素为 2,等于 2,found 变为 true,将 2 压入 commonStack,contains 方法返回 true。
将 2 压入 temp2。
stack2 为空,遍历结束。
恢复 stack2 的原始顺序:
从 temp2 中依次弹出元素并压入 stack2,stack2 恢复为 [2, 3, 4]。
转换结果:
将 commonStack 转换为 HashSet 去重,uniqueCommon 为 [2, 3]。
创建 result 数组,将 uniqueCommon 中的元素放入 result 数组,result 为 [2, 3]。

 

...全文
18 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

118

社区成员

发帖
与我相关
我的任务
社区描述
这里专为新疆政法学院的探索者而建,英雄不问出处。起跑线是起点,热忱与坚持为加速器,无论bug缠身的项目,还是攻克的算法顿悟,每滴汗水皆被珍藏。执炬前行,终将照亮彼此峰顶,我们携手同行。
课程设计笔记经验分享 高校 新疆·图木舒克市
社区管理员
  • 雲中203
  • SHAO060706
  • Poeir_
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

英雄不问出处

试试用AI创作助手写篇文章吧