3-哈希表-21-兩個數組的交集-LeetCode349
參考:代碼隨想錄
LeetCode: 題目序號349
更多內容歡迎關注我(持續更新中,歡迎Star?)
Github:CodeZeng1998/Java-Developer-Work-Note
技術公眾號:CodeZeng1998(純純技術文)
生活公眾號:好鍋(Life is more than code)
CSDN: CodeZeng1998
其他平臺:CodeZeng1998、好鍋
349. 兩個數組的交集
給定兩個數組 nums1
和 nums2
,返回 它們的 交集 。
輸出結果中的每個元素一定是 唯一 的。我們可以 不考慮輸出結果的順序 。
示例 1:
輸入:nums1 = [1,2,2,1], nums2 = [2,2]
輸出:[2]
示例 2:
輸入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
輸出:[9,4]
解釋:[4,9] 也是可通過的
提示:
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
解法一:使用 HashSet
/*** 兩個數組的交集** @param nums1 第一個數組* @param nums2 第二個數組* @return 有相交,返回交集,無相交,返回空數組* @author CodeZeng1998*/public int[] intersection(int[] nums1, int[] nums2) {if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {return new int[0];}Set<Integer> set = new HashSet<>();Set<Integer> intersection = new HashSet<>();for (int i : nums1) {set.add(i);}for (int i : nums2) {if (set.contains(i)) {intersection.add(i);}}int[] resultArray = new int[intersection.size()];int j = 0;for (Integer i : intersection) {resultArray[j++] = i;}return resultArray;}
ChatGPT:
這段代碼定義了一個名為intersection
的方法,接受兩個整數數組nums1
和nums2
作為輸入,并返回一個包含兩個數組交集的整數數組。兩個數組的交集是兩個數組共有的元素集合。
以下是代碼的詳細解釋:
- 方法首先檢查輸入數組是否為null或為空。如果任何一個為null或為空,它將返回一個空數組
{}
。 - 初始化一個名為
set
的HashSet,用于存儲第一個數組nums1
中的唯一元素。 - 另一個HashSet命名為
intersection
,用于存儲兩個數組中的公共元素。 - 第一個循環遍歷數組
nums1
中的每個元素,并將其添加到set
中。 - 第二個循環遍歷數組
nums2
中的每個元素。對于每個元素,它檢查它是否存在于set
中。如果存在,則將元素添加到intersection
集合中。 - 在找到公共元素之后,使用
intersection
集合的大小初始化一個整數數組resultArray
。 - 將
intersection
集合中的元素復制到resultArray
中。 - 最后,返回包含交集元素的
resultArray
。
解法二:使用 Hash 數組
- 注意題目的數組大小和數值范圍
/*** 兩個數組的交集** @param nums1 第一個數組* @param nums2 第二個數組* @return 有相交,返回交集,無相交,返回空數組* @author CodeZeng1998*/public int[] intersection(int[] nums1, int[] nums2) {int[] hash1Array = new int[1001];int[] hash2Array = new int[1001];for (int i : nums1) {hash1Array[i]++;}for (int i : nums2) {hash2Array[i]++;}List<Integer> resultList = new ArrayList<>();for (int i = 0; i < 1001; i++) {if (hash1Array[i] > 0 && hash2Array[i] > 0) {resultList.add(i);}}int[] resArray = new int[resultList.size()];int index = 0;for (Integer value : resultList) {resArray[index++] = value;}return resArray;}
ChatGPT:
這段代碼定義了一個名為intersection
的方法,接受兩個整數數組nums1
和nums2
作為輸入,并返回一個包含兩個數組之間交集元素的數組。
以下是代碼的詳細解釋:
int[] hash1Array = new int[1001];
和int[] hash2Array = new int[1001];
: 這些代碼初始化了兩個大小為1001的整數數組hash1Array
和hash2Array
。這些數組用于分別統計nums1
和nums2
中數字的出現次數。- 第一個循環遍歷了
nums1
的元素。對于每個元素i
,它在hash1Array
中將i
的計數遞增。 - 類似地,第二個循環遍歷了
nums2
的元素,并遞增hash2Array
中每個元素的計數。 - 處理了兩個數組之后,代碼從0到1000遍歷每個索引。對于每個索引
i
,如果hash1Array
和hash2Array
中i
的計數都大于0,則意味著i
同時存在于兩個數組中,因此將i
添加到名為resultList
的列表中。 - 在找到所有共同元素后,代碼使用
resultList
創建一個新的整數數組resArray
。 - 最后,它遍歷
resultList
并填充resArray
,然后返回resArray
。
349. Intersection of Two Arrays
Given two integer arrays nums1
and nums2
, return an array of their
intersection
. Each element in the result must be unique and you may return the result in any order.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Explanation: [4,9] is also accepted.
Constraints:
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
更多內容歡迎關注我(持續更新中,歡迎Star?)
Github:CodeZeng1998/Java-Developer-Work-Note
技術公眾號:CodeZeng1998(純純技術文)
生活公眾號:好鍋(Life is more than code)
CSDN: CodeZeng1998
其他平臺:CodeZeng1998、好鍋