給定兩個(gè)數(shù)組,編寫一個(gè)函數(shù)來計(jì)算它們的交集。
示例 1:
輸入: nums1 = [1,2,2,1], nums2 = [2,2]
輸出: [2,2]
示例 2:
輸入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
輸出: [4,9]
說明:
- 輸出結(jié)果中每個(gè)元素出現(xiàn)的次數(shù)死相,應(yīng)與元素在兩個(gè)數(shù)組中出現(xiàn)的次數(shù)一致。
- 我們可以不考慮輸出結(jié)果的順序囤躁。
****進(jìn)階:****
- 如果給定的數(shù)組已經(jīng)排好序呢常潮?你將如何優(yōu)化你的算法?
- 如果 _nums1 _的大小比 _nums2 _小很多揖盘,哪種方法更優(yōu)眉厨?
- 如果 _nums2 _的元素存儲(chǔ)在磁盤上,磁盤內(nèi)存是有限的兽狭,并且你不能一次加載所有的元素到內(nèi)存中憾股,你該怎么辦鹿蜀?
我自己想的算法,不依賴JDK現(xiàn)有的數(shù)據(jù)結(jié)構(gòu)服球,僅使用簡(jiǎn)單的數(shù)組結(jié)構(gòu)茴恰、while、for等循環(huán)
public static int[] intersect(int[] nums1, int[] nums2) {
int len1 = nums1.length, len2 = nums2.length;
if (len1 == 0 || len2 == 0) {
int[] aa = {};
return aa;
}
int notSameIndex = 0;
int tempSwapNum = 0;
int tempSameNum = 0;
int tempLesserTimes = 0;
// 是否出現(xiàn)過交集斩熊,一開始為false
boolean isIntersect = false;
if (len1 >= len2) {
for (int i = 0; i < nums2.length; i++) {
for (int j = 0; j < nums1.length; j++) {
// 相等往枣,而且要沒有判斷過的
if (nums2[i] == nums1[j] && isNotExist(isIntersect, notSameIndex - 1, nums2[i], nums2)) {
// 如果有交集,那么需要找出此元素在nums1,nums2中出現(xiàn)的次數(shù)粉渠,返回較小的值
tempSameNum = nums2[i];
tempLesserTimes = getLesserTimes(tempSameNum, nums1, nums2);
// 對(duì)nums2的元素進(jìn)行交換位置分冈,把交集部分移動(dòng)到數(shù)組前面
for (int i2 = notSameIndex; i2 < nums2.length; i2++) {
if (tempLesserTimes > 0 && tempSameNum == nums2[i2]) {
tempSwapNum = nums2[notSameIndex];
nums2[notSameIndex] = tempSameNum;
nums2[i2] = tempSwapNum;
notSameIndex++;
tempLesserTimes--;
}
}
isIntersect = true;
}
}
}
return Arrays.copyOf(nums2, notSameIndex);
} else {
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums2.length; j++) {
// 相等,而且要沒有判斷過的
if (nums1[i] == nums2[j] && isNotExist(isIntersect, notSameIndex - 1, nums1[i], nums1)) {
// 如果有交集霸株,那么需要找出此元素在nums1,nums2中出現(xiàn)的次數(shù)雕沉,返回較小的值
tempSameNum = nums1[i];
tempLesserTimes = getLesserTimes(tempSameNum, nums1, nums2);
// 對(duì)nums2的元素進(jìn)行交換位置,把交集部分移動(dòng)到數(shù)組前面
for (int i2 = notSameIndex; i2 < nums1.length; i2++) {
if (tempLesserTimes > 0 && tempSameNum == nums1[i2]) {
tempSwapNum = nums1[notSameIndex];
nums1[notSameIndex] = tempSameNum;
nums1[i2] = tempSwapNum;
notSameIndex++;
tempLesserTimes--;
}
}
isIntersect = true;
}
}
}
return Arrays.copyOf(nums1, notSameIndex);
}
}
private static boolean isNotExist(boolean isIntersect, int range, int num, int[] nums) {
if (isIntersect) {
boolean ret = true;
for (int i = 0; i < nums.length; i++) {
if (i <= range && nums[i] == num) {
ret = false;
break;
}
}
return ret;
} else {
return !isIntersect;
}
}
private static int getLesserTimes(int num, int[] nums1, int[] nums2) {
int times1 = 0;
for (int i = 0; i < nums1.length; i++) {
if (num == nums1[i]) {
times1++;
}
}
int times2 = 0;
for (int i = 0; i < nums2.length; i++) {
if (num == nums2[i]) {
times2++;
}
}
return Math.min(times1, times2);
}
下面這種算法是先對(duì)已知的兩個(gè)數(shù)組進(jìn)行排序去件,
public static int[] intersect2(int[] nums1, int[] nums2) {
Arrays.sort(nums1);
Arrays.sort(nums2);
List<Integer> list = new ArrayList<>();
for (int i = 0, j = 0; i < nums1.length && j < nums2.length;) {
if (nums1[i] < nums2[j]) {
i++;
} else if (nums1[i] > nums2[j]) {
j++;
} else {
list.add(nums1[i]);
i++;
j++;
}
}
int[] res = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
下面這種算法是使用 jdk現(xiàn)有的數(shù)據(jù)結(jié)構(gòu)坡椒,java.util.Map
public static int[] intersect3(int[] nums1, int[] nums2) {
Map<Integer, Integer> map1 = new HashMap<>();
for (Integer num : nums1)
if (map1.containsKey(num))
map1.replace(num, map1.get(num) + 1);
else
map1.put(num, 1);
ArrayList<Integer> intersection = new ArrayList<>(16);
for (Integer num : nums2) {
if (map1.containsKey(num) && map1.get(num) > 0) {
intersection.add(num);
map1.replace(num, map1.get(num) - 1);
}
}
int[] ret = new int[intersection.size()];
for (int i = 0; i < intersection.size(); i++) {
ret[i] = intersection.get(i);
}
return ret;
}