/*
* @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]\n[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=,flag=;
int lens=nums1Size>nums2Size?nums1Size:nums2Size;
int* re =(int*)malloc(sizeof(int)*lens);
if(nums1Size==) { * returnSize=k; return nums1; }
if(nums2Size==) { * returnSize=k; return nums2; }
for(i=;i<nums1Size;i++)
{//flag用来标识数组re中是否已经存在nums1和nums2的交集,flag==0,则只要比较nums2[j]==nums1[i]
if(flag==)
for(j=;j<nums2Size;j++)
{ if(nums2[j]==nums1[i])
{flag=;re[k++]=nums1[i];break;}
}
//flag==1说明re中已存在交集数字,则nums1[i]还需要与re数组中的每个元素进行比较,保证输出结果中的每个元素一定是唯一的
else if(flag==)
{int t,f=;
for (t=;t<k;t++)
if(nums1[i]==re[t]) { f=;break;}
if(f==) continue;
for(j=;j<nums2Size;j++)
{
if(nums2[j]==nums1[i])
{ flag=;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]\n[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去重然后一个&取交集。

Leecode刷题之旅-C语言/python-349两个数组的交集的更多相关文章

  1. Leecode刷题之旅-C语言/python-88合并两个有序数组

    /* * @lc app=leetcode.cn id=88 lang=c * * [88] 合并两个有序数组 * * https://leetcode-cn.com/problems/merge-s ...

  2. Leecode刷题之旅-C语言/python-21.合并两个有序链表

    /* * @lc app=leetcode.cn id=21 lang=c * * [21] 合并两个有序链表 * * https://leetcode-cn.com/problems/merge-t ...

  3. Leecode刷题之旅-C语言/python-67二进制求和

    /* * @lc app=leetcode.cn id=67 lang=c * * [67] 二进制求和 * * https://leetcode-cn.com/problems/add-binary ...

  4. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  5. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  6. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  7. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  8. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  9. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

随机推荐

  1. 【Leetcode】【Medium】Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  2. python 图形界面

    Python自带的库是支持Tk的Tkinter,使用Tkinter,无需安装任何包,就可以直接使用. Tk是一个图形库,支持多个操作系统 导入Tkinter包的所有内容: from tkinter i ...

  3. GO语言(八) defer注意点

    package main import ( "net" "os" "fmt" "io/ioutil" ) func Cl ...

  4. html5与html的区别

    最近看群里聊天聊得最火热的莫过于手机网站和html5这两个词.可能有人会问,这两者有什么关系呢?随着这移动互联网快速发展的时代,尤其是4G时代已经来临的时刻,加上微软对“XP系统”不提供更新补丁.维护 ...

  5. miniui dataGrid detail grid

    <div >      <div id="vkhGrjx_grid" class="mini-datagrid" style="wi ...

  6. UVa 1442 - Cave

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. HDU 3292 【佩尔方程求解 && 矩阵快速幂】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...

  8. centos7 python3.5安装mysqlclient1.3.9

    MySQL-python目前不支持python3.5,可以使用mysqlclient 下载地址:https://pypi.python.org/pypi/mysqlclient/1.3.9 解压后进入 ...

  9. springmvc和easyui使用ajax前台后台互传数据,假删除提示警告问题。

    前台 //删除 多/单条数据 function del(cid){ var id=''; if(cid=='-1'){ if(getSelections().length > 0){ id=ge ...

  10. 使用cmd命令创建vue(ivieiw)项目

    条件,安装好nodejs 第一步:先使用 vue create 命令创建一个项目,等待创建完成. 1.切换目录 2.创建项目  vue create [项目名称] 第二步:切换到项目中. 第三步:使用 ...