摘要:先想到的是,其實也可以,只是需要在遍歷的時候,添加到數(shù)組中的數(shù)要掉,略微麻煩了一點。在里跑的時候,也要快一點。另一種類似做法的就快的多了。如果是找出所有包括重復的截距呢
Problem
Given two arrays, write a function to compute their intersection.
NoticeEach element in the result must be unique.
The result can be in any order.
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
ChallengeCan you implement it in three different algorithms?
Note先想到的是HashSet(),其實HashMap也可以,只是需要在遍歷nums2的時候,添加到res數(shù)組中的數(shù)要remove掉,略微麻煩了一點。在LC里跑的時候,HashSet也要快一點。
另一種類似HashMap做法的BitSet()就快的多了。
public class Solution { public int[] intersection(int[] nums1, int[] nums2) { SetBitSetset1 = new HashSet(); Set set2 = new HashSet(); List ans = new ArrayList(); for (int i = 0; i < nums1.length; i++) set1.add(nums1[i]); for (int i = 0; i < nums2.length; i++) { if (set1.contains(nums2[i])) set2.add(nums2[i]); } int[] res = new int[set2.size()]; int index = 0; for (Integer i: set2) res[index++] = i; return res; } }
public class Solution { public int[] intersection(int[] nums1, int[] nums2) { int[] res = new int[nums1.length]; if (nums1.length == 0 || nums2.length == 0) return new int[0]; int index = 0; BitSet set = new BitSet(); for (int i = 0; i < nums1.length; i++) { set.set(nums1[i]); } for (int i = 0; i < nums2.length; i++) { if (set.get(nums2[i]) == true) { res[index++] = nums2[i]; set.set(nums2[i], false); } } return Arrays.copyOfRange(res, 0, index); } }Follow Up
如果是找出所有包括重復的截距呢?-- Intersection of Two Arrays II
public class Solution { public int[] intersect(int[] nums1, int[] nums2) { int k = 0, l1 = nums1.length, l2 = nums2.length; int[] result = new int[l1]; Arrays.sort(nums1); Arrays.sort(nums2); int i = 0, j = 0; while (i < l1 && j < l2) if (nums1[i] < nums2[j]) i++; else if (nums1[i] == nums2[j++]) result[k++] = nums1[i++]; return Arrays.copyOf(result, k); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/64776.html
Problem Write a program to find the node at which the intersection of two singly linked lists begins. Example The following two linked lists: A: a1 → a2 ↘ ...
摘要:由于要求的時間,所以選擇二分法。思路是找到兩個數(shù)組合并起來的第個元素。這樣只需計算兩個數(shù)組的中位數(shù)是第幾個元素,代入功能函數(shù)即可。據(jù)此,根據(jù)二分法的性質(zhì),我們在遞歸時可以將前即個元素排除。 Problem There are two sorted arrays A and B of size m and n respectively. Find the median of the tw...
摘要:首先將兩個字符串化成字符數(shù)組,排序后逐位比較,確定它們等長且具有相同數(shù)量的相同字符。然后,從第一個字符開始向后遍歷,判斷和中以這個坐標為中點的左右兩個子字符串是否滿足第一步中互為的條件設(shè)分為和,分為和。 Problem Given a string s1, we may represent it as a binary tree by partitioning it to two no...
摘要:建立一個長度為的數(shù)組,統(tǒng)計所有個字符在出現(xiàn)的次數(shù),然后減去這些字符在中出現(xiàn)的次數(shù)。否則,循環(huán)結(jié)束,說明所有字符在和中出現(xiàn)的次數(shù)一致,返回。 Program Write a method anagram(s,t) to decide if two strings are anagrams or not. Example Given s=abcd, t=dcab, return true....
Intersection of Two Arrays I Problem Given two arrays, write a function to compute their intersection. Example Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note Each element in the result m...
閱讀 1947·2023-04-26 02:32
閱讀 635·2021-11-18 13:12
閱讀 2516·2021-10-20 13:48
閱讀 2608·2021-10-14 09:43
閱讀 3914·2021-10-11 10:58
閱讀 3724·2021-09-30 10:00
閱讀 2997·2019-08-30 15:53
閱讀 3547·2019-08-30 15:53