From 0f458de25416863e592d419a35f535405610f9bf Mon Sep 17 00:00:00 2001 From: Terry Liu <102352821+Lozakaka@users.noreply.github.com> Date: Sun, 13 Aug 2023 22:59:49 -0400 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ejava=E8=A7=A3=E6=B3=95?= =?UTF-8?q?=EF=BC=88hashArray=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增java解法(hashArray) --- problems/0349.两个数组的交集.md | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/problems/0349.两个数组的交集.md b/problems/0349.两个数组的交集.md index 26a9286d..138067e3 100644 --- a/problems/0349.两个数组的交集.md +++ b/problems/0349.两个数组的交集.md @@ -121,7 +121,7 @@ public: ## 其他语言版本 ### Java: - +版本一:使用HashSet ```Java import java.util.HashSet; import java.util.Set; @@ -159,7 +159,28 @@ class Solution { } } ``` - +版本二:使用Hash數組 +```java +class Solution { + public int[] intersection(int[] nums1, int[] nums2) { + int[] hash1 = new int[1002]; + int[] hash2 = new int[1002]; + for(int i : nums1) + hash1[i]++; + for(int i : nums2) + hash2[i]++; + List resList = new ArrayList<>(); + for(int i = 0; i < 1002; i++) + if(hash1[i] > 0 && hash2[i] > 0) + resList.add(i); + int index = 0; + int res[] = new int[resList.size()]; + for(int i : resList) + res[index++] = i; + return res; + } +} +``` ### Python3: (版本一) 使用字典和集合