LeetCode:求两个数组的交集,相同数字允许出现多次

Given two arrays,write a function to compute their intersection.
Example 1:
Input:nums1=[1,2,2,1],nums2=[2,2]
Output:[2,2]

Example 2:
Input:nums1=[4,9,5],nums2=[9,4,9,8,4]
Output:[4,9]

Note:
(1)Each element in the result should appear as many times as it shows in both arrays.
(2)The result can be in any order.
Follow up:
(1)What if the given array is already sorted?How should you optimize your algorithm?
(2)What if num1's size is small compared to num2's size?Which algorithm is better?
(3)What if elements of nums2 are stored on disk,and the memory is limited such that
you cannot load all elements into the memory at once?

题目大意:
要求输出两个数组的交集元素,如果元素出现多次,则需要输出多次.
解题思路:
这道题利用字典可以达到最优解.把其中一组数子放到字典中,另外字典的key是数组中的数字,value是这个数字出现的次
数.在扫描另外一个数组的时候,每取出一个存在的数组,把字典中的value减去1.若value是0代表不存在.
package main

import "fmt"

func intersect(nums1,nums2 []int)[]int{
	ans,board:=[]int{},map[int]int{}
	if len(nums1)==0||len(nums2)==0{
		return ans
	}
	for _,num:=range nums1{
		_,ok:=board[num]
		if ok{
			board[num]++
		}else{
			board[num]=1
		}
	}
	for i:=0;i<len(nums2);i++{
		_,ok:=board[nums2[i]]
		if ok{
			ans=append(ans,nums2[i])
			if board[nums2[i]]!=1{
				board[nums2[i]]--
				continue
			}
			delete(board,nums2[i])
		}
	}
	return ans
}
func intersect1(nums1,nums2 []int)[]int{
	m:=map[int]int{}
	var res []int
	for _,n:=range nums1{
		m[n]++
	}
	for _,n:=range nums2{
		if m[n]>0{
			res=append(res,n)
			m[n]--
		}
	}
	return res
}
func main(){
	nums1,nums2:=[]int{4,9,5},[]int{9,4,9,8,4}
	fmt.Println(intersect(nums1,nums2))
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路上的追梦人

您的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值