Median of Two Sorted Arrays
Merge 2 sorted arrays
Intersection of Two Arrays
主要問題就是 i 指針和 j 指針
like A對應(yīng) i 指針,B對應(yīng) j 指針
不要寫亂 寫著寫著可能走神就寫的不對應(yīng)了 這種題不難 就怕你數(shù)組越界 一般都是 i, j 指針的問題
Merge Sorted Array
Example
A = [1, 2, 3, empty, empty], B = [4, 5]
After merge, A will be filled as [1, 2, 3, 4, 5]
這道題,兩個(gè)數(shù)組已經(jīng)是sorted的了,只是有個(gè)數(shù)組在后面有更多的空位
要倒著賦值 O(n)
正著的話每次移動整個(gè)數(shù)組都是O(n)的 worse case O(n^2)
public void mergeSortedArray(int[] A, int m, int[] B, int n) {
//把m - 1和n - 1單獨(dú)賦給 i 和 j ,在接下來寫程序的時(shí)候更好看,更好理解
int i = m - 1;
int j = n - 1;
int index = m + n - 1;
while (i >= 0 && j >= 0) {
if (A[i] > B[j]) {
A[index--] = A[i--];
} else {
A[index--] = B[j--];
}
}
while (i >= 0) {
A[index--] = A[i--];
}
while (j >= 0) {
A[index--] = B[j--];
}
}
下面的題目同理也是 i , j 指針的問題
Merge 2 sorted arrays
public int[] mergeSortedArray(int[] A, int[] B) {
if (A == null || B == null) {
return null;
}
int[] res = new int[A.length + B.length];
//while沒有像for一樣的現(xiàn)場賦值for(int i....),while都是提前賦好值的
int i = 0, j = 0, index = 0;
while (i < A.length && j < B.length) {
if (A[i] < B[j]) {
res[index++] = A[i++];
} else {
res[index++] = B[j++];
}
}
while (i < A.length) {
res[index++] = A[i++];
}
while (j < B.length) {
res[index++] = B[j++];
}
return res;
}
Intersection of Two Arrays
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
一、HashSet version
遍歷HashSet方法
1.迭代遍歷:
Set<String> set = new HashSet<String>();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String str = it.next();
System.out.println(str);
}
2.for循環(huán)遍歷:
for (String str : set) {
System.out.println(str);
}
HashSet version
Time:O(m + n)
Space:O(min(m, n))挑小一點(diǎn)的數(shù)組放到hashSet
public class Solution {
/*
* @param nums1: an integer array
* @param nums2: an integer array
* @return: an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
if (nums1 == null || nums2 == null) {
return null;
}
Set<Integer> hash = new HashSet<>();
for (int i = 0; i < nums1.length; i++) {
hash.add(nums1[i]);
}
Set<Integer> resultHash = new HashSet<>();
for (int i = 0; i < nums2.length; i++) {
if (hash.contains(nums2[i]) && !resultHash.contains(nums2[i])) {
resultHash.add(nums2[i]);
}
}
int size = resultHash.size();
int[] res = new int[size];
int index = 0;
for (Integer num : resultHash) {
res[index++] = num;
}
return res;
}
};
Version 2: sort & merge
Time: O(nlogn + mlogm + m + n)
Space: O(1)
數(shù)據(jù)結(jié)構(gòu):2 pointers
這種方法先排序
[1, 1, 2, 2]和[2, 2]
兩根指針從左往右掃,nums1[i] < nums2[j] 那么i++,看看下一個(gè)是否相等
nums1[i] > nums2[j] 那么j++
相等的話 如果沒有重復(fù)就放進(jìn)新的數(shù)組,之后要注意i , j的自增
一開始我們并不知道數(shù)組有多大,從nums1和nums2的長度里隨便挑一個(gè)
以加入的intersection元素的個(gè)數(shù)再導(dǎo)入到另一個(gè)新數(shù)組中,perfect!
public int[] intersection(int[] nums1, int[] nums2) {
Arrays.sort(nums1);//mlogm
Arrays.sort(nums2);//nlogn
int i = 0, j = 0;
int[] temp = new int[nums1.length];
int index = 0;
//這里的時(shí)間復(fù)雜度是m + n
while (i < nums1.length && j < nums2.length) {
if (nums1[i] == nums2[j]) {
if (index == 0 || temp[index - 1] != nums1[i]) {
temp[index++] = nums1[i];
}
//注意這里!i , j 指針要記得移動
i++;
j++;
} else if (nums1[i] < nums2[j]) {
i++;
} else {
j++;
}
}
int[] result = new int[index];
for (int k = 0; k < index; k++) {
result[k] = temp[k];
}
return result;
}
version 3: sort & binary search
這種方法先將nums1排序,然后對nums2中的每個(gè)元素,在nums1中二分查找,找到了放入HashSet中,再折騰進(jìn)數(shù)組
假設(shè)nums1.length = m
nums2.length = n
假設(shè)n < m 那么久QuickSort小的那個(gè),也就是n
Time: O(nlogn +mlogn) = (m + n)logn
Space: O(n)
public class Solution {
/*
* @param nums1: an integer array
* @param nums2: an integer array
* @return: an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
// write your code here
if (nums1 == null || nums2 == null) {
return null;
}
Set<Integer> hash = new HashSet<>();
Arrays.sort(nums1);//nlogn
for (int i = 0; i < nums2.length; i++) {//mlogn
if (set.contains(nums2[i])) {
continue;
}
if(binarySearch(nums2[i], nums1)) {
hash.add(nums2[i]);
}
}
int size = hash.size();
int[] res = new int[size];
int index = 0;
for (Integer num : hash) {
res[index++] = num;
}
return res;
}
private boolean binarySearch(int target, int[] num) {
if (num == null || num.length == 0) {
return false;
}
int start = 0;
int end = num.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (num[mid] == target) {
return true;
} else if (num[mid] < target) {
start = mid;
} else {
end = mid;
}
}
if (num[start] == target) {
return true;
}
if (num[end] == target) {
return true;
}
return false;
}
};
Median of Two Sorted Arrays
“There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays.”
The overall run time complexity should be O(log (m+n)).
如果k是中點(diǎn),那么k = (m+n)/2
則logk就是O(log (m+n))了
這道題看懂就可以了 我不追求會寫了:)
把這道題general成kth of 2 sorted arrays
因?yàn)橐髄og的時(shí)間復(fù)雜度,所以每次去掉一半,即去掉k/2, 兩個(gè)分別排序的數(shù)組,怎么去掉k/2個(gè)元素呢?
其實(shí)直接去掉A,B數(shù)組之一的k/2就可以了,比較一下A,B分別在k/2的值是多少,去掉小的那個(gè)數(shù)組的前k/2
如果數(shù)組非常短都沒有k/2的index,用MAX_VALUE代替,反正比的是誰小
如果數(shù)組一個(gè)數(shù)都沒有了,就去找另一個(gè)數(shù)組的第k個(gè)數(shù)
class Solution {
public double findMedianSortedArrays(int[] A, int[] B) {
int len = A.length + B.length;
//奇數(shù)個(gè):中間
if (len % 2 == 1) {
return findKth(A, 0, B, 0, len / 2 + 1);
}
//偶數(shù)個(gè), 是除以2.0, 否則取整了
return (findKth(A, 0, B, 0, len / 2) + findKth(A, 0, B, 0, len / 2 + 1)) / 2.0;
}
private static int findKth(int[] A, int A_start, int[] B, int B_start, int k) {
//A數(shù)組一個(gè)都沒有了
//k如果等于2,那么B_start = 0, (k - 1) = 1-----到k正好是第二個(gè)數(shù)
if (A_start >= A.length) {
return B[B_start + k - 1];
}
//B數(shù)組一個(gè)都沒有了
if (B_start >= B.length) {
return A[A_start + k - 1];
}
//k down to 1, return兩個(gè)數(shù)組中比較小的那個(gè)數(shù)
if (k == 1) {
return Math.min(A[A_start], B[B_start]);
}
//看A數(shù)組夠不夠長
int A_key;
//為什么要 - 1 呢?
//因?yàn)锳_start + k / 2 - 1就是從A_start開始的第k/2個(gè)數(shù),是一個(gè)index
if (A_start + k / 2 - 1 < A.length) {
A_key = A[A_start + k / 2 - 1];
} else {
A_key = Integer.MAX_VALUE;
}
int B_key;
if (B_start + k / 2 - 1 < B.length) {
B_key = B[B_start + k / 2 - 1];
} else {
B_key = Integer.MAX_VALUE;
}
if (A_key < B_key) {
//用k - k / 2而不用k/2是因?yàn)槌ㄓ袀€(gè)取整的問題,本題是去掉k/2個(gè)元素
//例如: k = 3, k/2 =1 然而k-k/2 = 2
return findKth(A, A_start + k / 2, B, B_start, k - k / 2);
} else {
return findKth(A, A_start, B, B_start + k / 2, k - k / 2);
}
}
}