From 0c19a22250d3f5a8328be6850297c36b25950056 Mon Sep 17 00:00:00 2001 From: aiueoriku <112373068+aiueoriku@users.noreply.github.com> Date: Thu, 22 May 2025 10:40:12 +0900 Subject: [PATCH] Create intersection of Two Arrays.md Problem https://leetcode.com/problems/intersection-of-two-arrays/description/ --- .../intersection of Two Arrays.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 intersection of Two Arrays/intersection of Two Arrays.md diff --git a/intersection of Two Arrays/intersection of Two Arrays.md b/intersection of Two Arrays/intersection of Two Arrays.md new file mode 100644 index 0000000..d207632 --- /dev/null +++ b/intersection of Two Arrays/intersection of Two Arrays.md @@ -0,0 +1,23 @@ +# Step1 +自力で解く +```python +class Solution: + def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: + intersection_nums =[] + for num1 in nums1: + if num1 not in intersection_nums and num1 in nums2: + intersection_nums.append(num1) + return intersection_nums +``` +ひとまずfor文で回した.辞書を使えばもっと計算効率上がるかも + +# Step2 +調べてみる.setという組み込み関数があるらしい +https://qiita.com/KueharX/items/a8cc7831fc430e9340ca +setはuniqueな値を辞書型で返すので,list()で括る +```python +class Solution: + def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: + intersected_nums = list(set(nums1) & set(nums2)) + return intersected_nums +```