当前位置: 首页 > news >正文

LintCode第547题-两数组的交集

描述

给出两个数组,写出一个方法求出它们的交集

结果中的每个元素必须是唯一的

例1:

输入: nums1 = [1, 2, 2, 1], nums2 = [2, 2], 
输出: [2].

例2:

输入: nums1 = [1, 2], nums2 = [2], 
输出: [2].

挑战

可以用三种不同的方法实现吗?

思路:由于元素必须唯一 那么我们立即想到Set 和map集合类 其中结果要求返回仅仅是去重数字 所以用Set方法最高效去重 

主要步骤是1 去重    2 比较相等并记录返回

代码如下:

public class Solution {

    /**

     * @param nums1: an integer array

     * @param nums2: an integer array

     * @return: an integer array

     *          we will sort your return value in output

     */

            public int[] intersection(int[] nums1, int[] nums2) {

            // write your code here

            //两个循环

            int m=nums1.length;

            int n=nums2.length;

            int[] nums3 = new int[m];

            Set<Integer> nums1Set=new HashSet<>();

            Set<Integer> nums2Set=new HashSet<>();

            //去重

            for (int x : nums1)

            {

                nums1Set.add(x);

            }

            for (int y : nums2)

            {

                nums2Set.add(y);

            }

            int Index=0;

            int nums3Length=0;


               // 【新增】先数一遍真实交集个数(保持你的双循环+break 结构)

        for (Integer eachNums1 : nums1Set) {

            Integer temp = eachNums1;

            for (Integer eachNums2 : nums2Set) {

                if (temp.equals(eachNums2)) {

                    nums3Length++;

                    break;

                }

            }

        }

            //拿出对应的元素

            nums3=new int[nums3Length];

            for(Integer eachNums1:nums1Set)

            {

                Integer temp=eachNums1;

                for(Integer eachNums2:nums2Set)

                {

                    if(temp.equals(eachNums2))

                    {

                        nums3[Index++]=temp;

                         break;

                    }

                }

            }

            return nums3;

        }

    }

其中比较相等为双层for循环所以 时间复杂度为O(m*n)

如果要优化后的

那么关键在于 把第二次循环变为了哈希查找 时间复杂度变为了O(m+n) 为什么会变成这样 因为

哈希分布时几乎总是 O(1)

代码如下:

import java.util.*;

public class Solution {

    public int[] intersection(int[] nums1, int[] nums2) {

        if (nums1 == null || nums2 == null) return new int[0];

        Set<Integer> nums1Set = new HashSet<>();

        Set<Integer> nums2Set = new HashSet<>();//下面两句时间复杂度为O(m+n)

        for (int x : nums1) nums1Set.add(x);

        for (int y : nums2) nums2Set.add(y);

        // 选小集合作为外层,contains 判交集  通过判断可以判断是返回的长度

        Set<Integer> smallSet;

        Set<Integer> bigSet;

        if (nums1Set.size() <= nums2Set.size()) {

            smallSet = nums1Set;

            bigSet = nums2Set;

        } else {

            smallSet = nums2Set;

            bigSet = nums1Set;

        }

        // 计数

        int returnLength = 0;

        for (Integer v : smallSet) //时间复杂度为O(min(m, n))

        {

            if (bigSet.contains(v))

         {

             returnLength++;

         }

        }

        // 分配并填充

        int[] nums3 = new int[returnLength];

        int returnIndex = 0;

        for (Integer v : smallSet)// 时间复杂度为O(min(m, n))

        {

            if (bigSet.contains(v))

            {

                nums3[returnIndex++] = v;

            }

        }

        return nums3;

    }

}

则最终时间复杂度为Math.max(O(min(m, n)),O(m+n))即O(m+n)

http://www.dtcms.com/a/323592.html

相关文章:

  • leetcode 49. 字母异位词分组 - java
  • [激光原理与应用-202]:光学器件 - 增益晶体 - Nd:YVO₄增益晶体的制造过程与使用过程
  • vite面试题及详细答案120题(61-90)
  • 简单聊聊PowerShell
  • Effective C++ 条款32:确定你的public继承塑模出 is-a 关系
  • 【读代码】深度解析 SmolAgents Open Deep Research
  • 杰理-AW-断言-log
  • 计算机网络基础(三)
  • [Shell编程] Shell 函数
  • PyQt5技术栈简述
  • .htaccess 文件上传漏洞绕过总结
  • Linux文件操作详解:一切皆文件
  • 编辑距离-二维动态规划
  • SkyWalking-3--Java Agent开发和集成示例
  • AI智能编程工具汇总
  • ComfyUI版本更新---解决ComfyUI的节点不兼容问题
  • MySQL 主备(Master-Slave)复制 的搭建
  • SOLIDWORKS 2025对工具栏等进行了重新布局和优化
  • GoEnhance AI-AI视频风格转换工具
  • gRPC 全面解析与实战 —— 从原理到同步/异步开发全攻略
  • Linux系统编程——进程地址空间
  • GM3568JHF:FPGA+ARM异构开发板环境搭建教程
  • 嵌入式学习day23-shell命令
  • Qdrant Filtering:must / should / must_not 全解析(含 Python 实操)
  • 【Python 高频 API 速学 ②】
  • 【线程池】压测确定线程池合适的参数
  • 【js】判断异步函数的返回值要加await
  • 使用LangGraph从零构建多智能体AI系统:实现智能协作的完整指南
  • 计算机系统设计中都有什么任务~计算密集~IO密集~逻辑密集等
  • 提示条贴合右侧边栏