添加0349.两个数组的交集 Ruby实现

This commit is contained in:
han
2023-07-27 16:27:19 +08:00
parent 199827b6c2
commit 7ee623f4da

View File

@ -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/)