当前位置: 首页 > 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.lryc.cn/news/615472.html

相关文章:

  • 腾讯COS云存储入门
  • 浅尝AI辅助C转Verilog方法
  • 新手小白使用jQuery在实际开发中常用到的经验
  • 第二十天:余数相同问题
  • 《Resolving tissue complexity by multimodal spatial omics modeling with MISO》
  • 【面试场景题】微博热点新闻系统设计方案
  • day18 - CSS函数
  • nginx高性能web服务器
  • 基于Prometheus、Grafana、Loki与Tempo的统一监控平台故障排查与解决方案
  • java组件安全vulhub靶场
  • [激光原理与应用-206]:光学器件 - SESAM - 基本结构与工作原理
  • 通用AGI到来,记忆仍需要一点旧颜色
  • 【Python 高频 API 速学 ⑦ · 完结篇】
  • 【31】C#实战篇——获取路径下的文件名(不包含路径和扩展名),并分离出文件名`fileName` ,文件名编号`SN`,文件名前缀`WMT`
  • 智能情趣设备、爆 bug:可被远程操控。。。
  • GPT-5深度解析:革命性AI模型的全面报告与实战指南
  • Linux Makefile解析
  • 车流高峰漏检率↓85%!陌讯时序建模方案在智慧交通的实时优化​
  • Netbsd安装使用
  • Ubuntu下搭建LVGL模拟器
  • [SC]高效地调试SystemC模型中的语法错误
  • actuary notes[1]
  • urmom damn the jvm
  • C++2024 年一级
  • 基于 InfluxDB 的服务器性能监控系统实战(一)
  • P1053 [NOIP 2005 提高组] 篝火晚会
  • Linux学习--软件编程(shell命令)
  • 多线程(四) --- 线程安全问题
  • 使用 Ansys Discovery 进行动态设计和分析
  • js零基础入门