From 7ee623f4da4e6615f867d2456a6ec174362f0100 Mon Sep 17 00:00:00 2001 From: han Date: Thu, 27 Jul 2023 16:27:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A00349.=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86=20Ruby=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0349.两个数组的交集.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 8daf5a35..26a9286d 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -465,6 +465,25 @@ object Solution { } ``` + +###Ruby +```ruby +def intersection(nums1, nums2) + hash = {} + result = {} + + nums1.each do |num| + hash[num] = 1 if hash[num].nil? + end + + nums2.each do |num| + #取nums1和nums2交集 + result[num] = 1 if hash[num] != nil + end + + return result.keys +end +``` ## 相关题目 * [350.两个数组的交集 II](https://leetcode.cn/problems/intersection-of-two-arrays-ii/)