zoukankan      html  css  js  c++  java
  • Leecode刷题之旅-C语言/python-349两个数组的交集

    /*
     * @lc app=leetcode.cn id=349 lang=c
     *
     * [349] 两个数组的交集
     *
     * https://leetcode-cn.com/problems/intersection-of-two-arrays/description/
     *
     * algorithms
     * Easy (60.49%)
     * Total Accepted:    15.1K
     * Total Submissions: 25K
     * Testcase Example:  '[1,2,2,1]
    [2,2]'
     *
     * 给定两个数组,编写一个函数来计算它们的交集。
     * 
     * 示例 1:
     * 
     * 输入: nums1 = [1,2,2,1], nums2 = [2,2]
     * 输出: [2]
     * 
     * 
     * 示例 2:
     * 
     * 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
     * 输出: [9,4]
     * 
     * 说明:
     * 
     * 
     * 输出结果中的每个元素一定是唯一的。
     * 我们可以不考虑输出结果的顺序。
     * 
     * 
     */
    /**
     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
        int i,j,k=0,flag=0;
        int lens=nums1Size>nums2Size?nums1Size:nums2Size;
        int* re =(int*)malloc(sizeof(int)*lens);
        if(nums1Size==0) { * returnSize=k; return nums1; }
        if(nums2Size==0) { * returnSize=k; return nums2; }
     for(i=0;i<nums1Size;i++)
        {//flag用来标识数组re中是否已经存在nums1和nums2的交集,flag==0,则只要比较nums2[j]==nums1[i]
        if(flag==0)
             for(j=0;j<nums2Size;j++)
               { if(nums2[j]==nums1[i]) 
                   {flag=1;re[k++]=nums1[i];break;} 
               }      
    //flag==1说明re中已存在交集数字,则nums1[i]还需要与re数组中的每个元素进行比较,保证输出结果中的每个元素一定是唯一的           
        else if(flag==1)
         {int t,f=0;
                   for (t=0;t<k;t++) 
                       if(nums1[i]==re[t]) { f=1;break;}
           if(f==1) continue;
              for(j=0;j<nums2Size;j++)    
                {
                  if(nums2[j]==nums1[i]) 
                  { flag=1;re[k++]=nums1[i];break;}
                }        
           }
        }   
        * returnSize=k;
        return re;
    }

    思路是 建立第三个数组,其长度为两个数组中较小的长度的那个。然后判断是否空集。(这么理解吧=。=)

    然后就是 在第二个数组中逐一选择与第一个数组中数对比,如果相等的话就存入第三个数组。flag用来标识数组re中是否已经存在nums1和nums2的交集,flag==0,则只要比较nums2[j]==nums1[i]

    flag==1说明re中已存在交集数字,则nums1[i]还需要与re数组中的每个元素进行比较,保证输出结果中的每个元素一定是唯一的   

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    python:

    #
    # @lc app=leetcode.cn id=349 lang=python3
    #
    # [349] 两个数组的交集
    #
    # https://leetcode-cn.com/problems/intersection-of-two-arrays/description/
    #
    # algorithms
    # Easy (60.49%)
    # Total Accepted:    15.1K
    # Total Submissions: 25K
    # Testcase Example:  '[1,2,2,1]
    [2,2]'
    #
    # 给定两个数组,编写一个函数来计算它们的交集。
    # 
    # 示例 1:
    # 
    # 输入: nums1 = [1,2,2,1], nums2 = [2,2]
    # 输出: [2]
    # 
    # 
    # 示例 2:
    # 
    # 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
    # 输出: [9,4]
    # 
    # 说明:
    # 
    # 
    # 输出结果中的每个元素一定是唯一的。
    # 我们可以不考虑输出结果的顺序。
    # 
    # 
    #
    class Solution:
        def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
            return list(set(nums1) & set(nums2))

    python就很骚,直接两个set去重然后一个&取交集。

  • 相关阅读:
    python 利用爬虫获取页面上下拉框里的所有国家
    python3 requests 进行接口测试、爬虫使用总结
    Tomcat设置最佳线程数总结
    Java中实现对象的比较:Comparable接口和Comparator接口
    堆排序算法
    快速排序算法2---以第一个元素作为主元
    字符串匹配---暴力匹配算法
    快速排序算法
    Spring中@Autowired注解与自动装配
    Spring的注解
  • 原文地址:https://www.cnblogs.com/lixiaoyao123/p/10556773.html
Copyright © 2011-2022 走看看